Unity3d 如何将Unity.Object强制转换为泛型类成员?

Unity3d 如何将Unity.Object强制转换为泛型类成员?,unity3d,Unity3d,我是一个几十岁的C程序员。Unity是我对现代C语言的第一次尝试,我发现自己惊喜不已——但语言的一些微妙之处却让我不知所措 首先是一个技术问题:在泛型类中存储Unity对象的正确方法是什么?在下面的示例中,我得到以下错误: Assets/scripts/MediaTest.cs(49,44): error CS0030: Cannot convert type `UnityEngine.Texture2D' to `T' 第二个真正的问题是:加载一组纹理的更好方法是什么 提前谢谢你的帮助 /*

我是一个几十岁的C程序员。Unity是我对现代C语言的第一次尝试,我发现自己惊喜不已——但语言的一些微妙之处却让我不知所措

首先是一个技术问题:在泛型类中存储Unity对象的正确方法是什么?在下面的示例中,我得到以下错误:

Assets/scripts/MediaTest.cs(49,44): error CS0030: Cannot convert type `UnityEngine.Texture2D' to `T'
第二个真正的问题是:加载一组纹理的更好方法是什么

提前谢谢你的帮助

/*
 * MediaTest.cs
 *
 * Pared down generic class test
 *
 */

using System.Collections;                   // Load IEnumerable
using System.Collections.Generic;           // Load Dictionary
using System.Text.RegularExpressions;       // Load Regex
using UnityEngine;                          // Load UnityEngine.Object

public class MediaTest<T> : IEnumerable where T : UnityEngine.Object {
    private Dictionary<string, MediaContent> _dict =
        new Dictionary<string, MediaContent>();

    private class MediaContent {
        public string path { get; set; }
        public T item { get; set; }

        public MediaContent(string path, T item) {
            this.path = path;
            this.item = item;
        }
    }

    // Indexer to return a UnityEngine.Object by filename
    public T this[string name] {
        get { return (T)_dict[name].item; }
    }

    // Convert a path to just the filename
    public string Basename(string path) {
        return new Regex(@"^.*/").Replace(path, "");
    }

    // Iterate through the filenames (keys) to the stored objects
    public IEnumerator GetEnumerator() {
        foreach (string name in _dict.Keys) {
            yield return name;
        }
    }

    // Read in the Resource at the specified path into a UnityEngine.Object
    public void Load(string path, bool load=false) {
        string name = Basename(path);
        if (this.GetType() == typeof(Media<Texture2D>) && IsStill(name)) {
            T item = (load) ? (T)Resources.Load<Texture2D>(path) : null;
            _dict[name] = new MediaContent(path, item);
            return;
        }
        if (this.GetType() == typeof(Media<AudioClip>) && IsAudio(name)) {
            T item = (load) ? (T)Resources.Load<AudioClip>(path) : null;
            _dict[name] = new MediaContent(path, item);
            return;
        }
    }

    // The real code uses Regex.Match on the file extension for supported types
    public bool IsStill(string name) { return true; }
    public bool IsAudio(string name) { return true; }
}
/*
*中科院
*
*精简泛型类测试
*
*/
使用System.Collections;//负载可数
使用System.Collections.Generic;//加载字典
使用System.Text.RegularExpressions;//加载正则表达式
使用UnityEngine;//加载UnityEngine.Object
公共类MediaTest:IEnumerable其中T:UnityEngine.Object{
私人词典=
新字典();
私有类媒体内容{
公共字符串路径{get;set;}
公共T项{get;set;}
公共媒体内容(字符串路径,T项){
this.path=path;
this.item=项目;
}
}
//按文件名返回UnityEngine.Object的索引器
public T此[字符串名称]{
获取{return(T)_dict[name].item;}
}
//将路径转换为文件名
公共字符串基名称(字符串路径){
返回新的正则表达式(@“^.*/”)。替换(路径“”);
}
//遍历存储对象的文件名(键)
公共IEnumerator GetEnumerator(){
foreach(dict.Keys中的字符串名称){
产生返回名称;
}
}
//将指定路径处的资源读入UnityEngine.Object
公共无效加载(字符串路径,bool Load=false){
字符串名称=基本名称(路径);
if(this.GetType()==typeof(媒体)和&IsStill(名称)){
T item=(加载)?(T)Resources.load(路径):null;
_dict[name]=新媒体内容(路径、项目);
返回;
}
if(this.GetType()==typeof(媒体)和&IsAudio(名称)){
T item=(加载)?(T)Resources.load(路径):null;
_dict[name]=新媒体内容(路径、项目);
返回;
}
}
//真正的代码在支持的类型的文件扩展名上使用Regex.Match
public bool IsStill(字符串名){return true;}
public bool IsAudio(字符串名){return true;}
}

以下是工作代码以及注释中的更新。谢谢德胡戈

    public void Load(string path, bool load=false) {
        // Translate the filesystem path to a Resources relative path by removing both
        // The */Resources prefix and the filename extention
        Regex re_path = new Regex(@".*/Resources/");
        Regex re_ext = new Regex(@"\.\w+$");
        string relpath = re_ext.Replace(re_path.Replace(path, ""), "");

        // Create an asset name from the path
        string name = System.IO.Path.GetFileName(relpath);

        // Skip this file if it doesn't match our type
        if (    (typeof(T) == typeof(Texture2D) && IsStill(path)) ||
             // (typeof(T) == typeof(Video) && IsVideo(path)) ||
                (typeof(T) == typeof(AudioClip) && IsAudio(name))) {
            T item = (load) ? Resources.Load(relpath) as T : null;
            _dict[name] = new MediaContent(path, item);
        }
    }

根据,以下更改似乎有效:``T item=(加载)?加载(路径)为T:null;`这合法吗?不
Resources.Load
反正已经返回给定类型了吗?您可以简单地使用
T item=load?Resources.Load(path):null没有这些特定的类型检查…Resources.Load返回type,而T匹配type Nope。。您传入type
Texture2D
so
Resources.Load(path)
返回一个
Texture2D
T可以是任何单位对象,因此您无法将Texture2D转换为T。例如,如果T是材质,您要求编译器将Texture2D转换为材质。这是办不到的。在我看来,您应该用Unity对象替换T,而不要使用泛型。关于你的第二个问题,你能详细说明这个脚本的用途吗?