当我运行 unity2D 代码时,图像未加载到屏幕上

问题描述 投票:0回答:1

我的代码的这一部分应该生成一行图像,我保存为表示为网格,我对 unity 和 id 很陌生,喜欢一些关于为什么此代码不起作用的帮助。我这段代码的目的是每当使用“AddRow”过程时生成行。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Properties;
using UnityEngine;
using UnityEngine.SocialPlatforms.GameCenter;

namespace Assets
{
    public class GridGen : MonoBehaviour
    {
        // list to keep track of which row has already been generated
        private List<bool> boolList = new List<bool>();
        private int width = 7;
        private float tileSize = 1;
        // made to clear bool list and generated blocks when game restarts
        public void Restart()
        { 
            
        }
        public void AddRow(char material, int row)
        {
            GameObject referencetile;
            if (material == 'g')
            {
                referencetile = (GameObject)Instantiate(Resources.Load("grass"));
                //gets grass block image
            }
            else
            {
                referencetile = (GameObject)Instantiate(Resources.Load("Road"));
                //gets road image
            }
            for (int i = 0; i < width; i++)
            {
                GameObject tile = (GameObject)Instantiate(referencetile, transform);

                float posY = row * tileSize;
                float posX = (i - 3) * tileSize;

                tile.transform.position = new Vector2(posX, posY);
            }
            Destroy(referencetile);

        }

        


    }
}```


i used chat gbt to try find the problem but they only said that i may of typed the wrong value for the names of the pictures
c# unity-game-engine 2d
1个回答
0
投票

我猜您可能面临一个甚至两个问题:

  1. 您的预制件名称有问题。检查它们是否正确并对应于您作为参数传递到 Resources.Load() 方法中的字符串。这是 Unity 的官方文档 https://docs.unity3d.com/ScriptReference/Resources.Load.html。请注意,您尝试生成的预制件必须位于 Resources 文件夹中。
  2. 如果您使用 2D 相机,请尝试更改它的 z 轴。也许你的图像在它后面被实例化了。
© www.soinside.com 2019 - 2024. All rights reserved.