简体   繁体   English

无法在 Unity 编辑器中加载预制件

[英]Can't load prefab in Unity Editor

I am trying to make an [ExecuteInEditMode] script spawn game objects (linked to the same prefab) at specific positions right in the Editor so that I can quickly create different hexagon tile maps by just triggering booleans in the inspector.我正在尝试在编辑器中的特定位置制作一个 [ExecuteInEditMode] 脚本生成游戏对象(链接到同一个预制件),以便我可以通过在检查器中触发布尔值来快速创建不同的六边形图块地图。 However, the Resources.Load() method cannot not find the prefab even though the path is correct and so I get the following error:但是,即使路径正确,Resources.Load() 方法也找不到预制件,因此出现以下错误:

NullReferenceException: Object reference not set to an instance of an object. NullReferenceException:未将对象引用设置为对象的实例。

Here is the code:这是代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[ExecuteInEditMode]
public class PositionChecker : MonoBehaviour
{
    [SerializeField] float tileGap = 1.5f;
    [SerializeField] GameObject tilePrefab; // alternatively tried dragging the prefab in the field in the inspector - it worked

    [SerializeField] bool tileUpLeft;

    GameObject tilesParent;

    private void Awake()
    {
        tilesParent = GameObject.Find("All Tiles");
        tilePrefab = Resources.Load("Assets/Prefabs/Tile.prefab") as GameObject;
    }

    // Update is called once per frame
    void Update()
    {
        CheckForCreateTile();
    }

    private void CheckForCreateTile()
    {
        if (tileUpLeft)
        {   
            tileUpLeft = false;
            InstantiateTilePrefab(new Vector3(transform.position.x - 0.6f * tileGap, transform.position.y, transform.position.z - tileGap));
        }
    }

    private void InstantiateTilePrefab(Vector3 vector3)
    {
        GameObject newTile = PrefabUtility.InstantiatePrefab(tilePrefab, tilesParent.transform) as GameObject;
        Debug.Log(tilePrefab); // null
        Debug.Log(tilesParent); // ok
        Debug.Log(newTile); // Null
        newTile.transform.position = vector3;
    }

}


If I drag the prefab onto the serialized field of each created tile manually in the inspector instead of trying to load it, everything works fine.如果我在检查器中手动将预制件拖到每个创建的图块的序列化字段上,而不是尝试加载它,则一切正常。

The asset has to be in a "Resources" folder.资产必须位于“资源”文件夹中。 So to solve your problem you can put "Tile.prefab" into the folder "Assets/Resources" and use the relative path: Resources.Load("Tile.prefab");因此,要解决您的问题,您可以将“Tile.prefab”放入文件夹“Assets/Resources”并使用相对路径: Resources.Load("Tile.prefab");

https://docs.unity3d.com/ScriptReference/Resources.Load.htmlhttps://docs.unity3d.com/ScriptReference/Resources.Load.html

The file-path string automatically starts with "Resources/".文件路径字符串自动以“Resources/”开头。 So you shouldn't type that in. Also I don't think unity likes the file type in the string so don't type that in either.所以你不应该输入它。另外,我认为 unity 不喜欢字符串中的文件类型,所以不要输入它。 This works for me:这对我有用:

GameObject tilePrefab;
tilePrefab = Resources.Load<GameObject>("Prefabs/Tile");

GameObject tile = Instantiate(tilePrefab, Vector3.zero, Quaternion.identity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM