Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将资产目录中的所有预置都列为预置而不是字符串? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用System.IO; 公共类预制:单行为 { 列表预置=新列表(); //用于初始化 无效开始() { var resourcesPath=Application.dataPath; var absolutePaths=System.IO.Directory.GetFiles(resourcesPath,“*.prefa”,System.IO.SearchOption.AllDirectories); foreach(绝对路径中的var absolutePath) { var prefab=Path.GetFileName(绝对路径); 预制件。添加(预制件); } } //每帧调用一次更新 无效更新(){ } }_C#_Unity3d_Unity5 - Fatal编程技术网

C# 如何将资产目录中的所有预置都列为预置而不是字符串? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用System.IO; 公共类预制:单行为 { 列表预置=新列表(); //用于初始化 无效开始() { var resourcesPath=Application.dataPath; var absolutePaths=System.IO.Directory.GetFiles(resourcesPath,“*.prefa”,System.IO.SearchOption.AllDirectories); foreach(绝对路径中的var absolutePath) { var prefab=Path.GetFileName(绝对路径); 预制件。添加(预制件); } } //每帧调用一次更新 无效更新(){ } }

C# 如何将资产目录中的所有预置都列为预置而不是字符串? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用System.IO; 公共类预制:单行为 { 列表预置=新列表(); //用于初始化 无效开始() { var resourcesPath=Application.dataPath; var absolutePaths=System.IO.Directory.GetFiles(resourcesPath,“*.prefa”,System.IO.SearchOption.AllDirectories); foreach(绝对路径中的var absolutePath) { var prefab=Path.GetFileName(绝对路径); 预制件。添加(预制件); } } //每帧调用一次更新 无效更新(){ } },c#,unity3d,unity5,C#,Unity3d,Unity5,问题是变量prefate是一个字符串,而列表是GameObject类型。我想要得到的不是字符串,而是预设 更新: 我将所有预制件移动到路径Asstes/Resources 但我得到了0个预制件 using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class Prefabs : MonoBehaviour { List<Game

问题是变量prefate是一个字符串,而列表是GameObject类型。我想要得到的不是字符串,而是预设

更新:

我将所有预制件移动到路径Asstes/Resources 但我得到了0个预制件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class Prefabs : MonoBehaviour
{

    List<GameObject> prefabs = new List<GameObject>();

    // Use this for initialization
    void Start ()
    {
        var resourcesPath = Application.dataPath;
        var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);
        foreach (var absolutePath in absolutePaths)
        {
            var prefab = Path.GetFileName(absolutePath);
            prefabs.Add(prefab);
        }
    }

    // Update is called once per frame
    void Update () {

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.IO;
公共类预制:单行为
{
//用于初始化
无效开始()
{
var pres=Resources.LoadAll(“资产/资源/”);
}
}
变量pres为空。
我想获得文件夹资源中的所有预置和资源中的子目录。

您可以创建一个新类,它派生自GameObject类。在新类中,可以添加字符串属性

例如:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class Prefabs : MonoBehaviour
{
    // Use this for initialization
    void Start ()
    {
        var pres = Resources.LoadAll<GameObject>("Assets/Resources/");
    }
}
然后你可以使用你的类,像这样:

public class MyGameObject : GameObject{

   public MyGameObject() : base(){

       Console.WriteLine("Calls constructor from base class and this!");
   }

   public string MyPath {get; set;}
}

如果您想使用该星座,请查看此处:

假设您使用的是Unity5,请查看AssetDatabase.LoadAssetPath

…从链接页面:

foreach (var absolutePath in absolutePaths)
{
    var prefab = Path.GetFileName(absolutePath);
    prefabs.Add(new MyGameObject(){MyPath = prefab);
}
页面上有一个关于如何使用的很好的解释。请注意注释,因为“仅使用前斜杠”之类的内容非常重要! 还请注意,这是在UnityEdit命名空间中(通常不包括在构建中),因此您可能希望在其周围有一个
#if

static void ImportExample()
    {
        Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D));
    }

这是用于编辑器插件还是独立构建?@Programmer我将在稍后使用它,例如从脚本中将部分预制添加到地形树中。
GameObject
是一个内置的Unity类-OP不会使用构造函数实例化它,也不能直接向它添加新字段。
static void ImportExample()
    {
        Texture2D t = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg", typeof(Texture2D));
    }
#if (UNITY_EDITOR) 
... your class/code ...
#endif