栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity .Asset文件出现“The associated script can not be loaded. Please fix any compile errors and assi...“

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity .Asset文件出现“The associated script can not be loaded. Please fix any compile errors and assi...“

Unity .Asset文件出现“The associated script can not be loaded. Please fix any compile errors and assi...
    • 问题背景:
    • 处理方案:
    • 项目地址:

问题背景:
用到Unity Asset 发现PC-Editor运行正常 但是一打包安卓就发现找不到文件的情况
处理方案:

只要保证如图三个地方名称一致即可
问题核心代码:

MyAssetEditor.cs:

public class MyAssetEditor : UnityEditor.Editor
    {
        [MenuItem("MyAsset/CreateAsset")]
        static void Create()
        {
            CreateAsset();
        }

        /// 
        /// 创建Asset
        /// 
        public static void CreateAsset()
        {
            // 判断目录是否存在
            if (!Directory.Exists(MyAssetHelper.GetFolderPath()))
            {
                Directory.CreateDirectory(MyAssetHelper.GetFolderPath());
            }

            // 创建.asset文件
            MyAsset _asset = MyAsset.CreateInstance();
            AssetDatabase.CreateAsset(_asset, MyAssetHelper.GetFilePath());
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("Asset create success");
        }
    }

MyAsset.cs

	[Serializable]
    public enum UserAgeType
    {
        Unknown, // 错误类型
        Child, // 儿童
        Adult, // 成人
        Elder, // 老人
    }

    [Serializable]
    public enum UserSexType
    {
        Unknown, // 错误类型
        Male, // 雄性
        Female, // 雌性
    }

    [Serializable]
    public class User
    {
        public string Name;
        public UserAgeType AgeType;
        public UserSexType SexType;

        public User(string name, UserAgeType ageTp, UserSexType sexTp) {
            this.Name = name;
            this.AgeType = ageTp;
            this.SexType = sexTp;
        }

        public override string ToString()
        {
            return this.Name + "," + this.AgeType + "," + this.SexType;
        }
    }

    public class MyAsset : ScriptableObject
    {
        [SerializeField]
        public User Author = new User("东宝",UserAgeType.Adult,UserSexType.Male); 
    }

    public static class MyAssetHelper
    {
        private const string FolderPath = "Assets/Resources";

        private const string FileName = "MyAsset";

        public static string GetFilePath()
        {
            return string.Format("{0}/{1}.asset", FolderPath, FileName);
        }

        public static string GetFileName()
        {
            return FileName;
        }

        public static string GetFolderPath()
        {
            return FolderPath;
        }
    }

Sample.cs

public class Sample : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        MyAsset.MyAsset _asset = Resources.Load(MyAsset.MyAssetHelper.GetFileName());
        Debug.Log(_asset.Author.ToString());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
项目地址:

MyAsset

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号