コード例 #1
0
        public static T Generate <T>(string assetPath = null, bool log = true) where T : ScriptableObject
        {
            assetPath = Path.ChangeExtension(string.IsNullOrEmpty(assetPath) ? DefaultAssetPath : assetPath, AssetFileExtension);

            var instance = AssetDatabase.LoadAssetAtPath <T>(assetPath);

            if (instance == null)
            {
                var projectPath = UnityPathUtility.ConvertProjectPath(assetPath);
                var path        = PathUtility.Combine(Application.dataPath, projectPath);

                if (!File.Exists(path))
                {
                    AssetDatabase.CreateAsset(CreateInstance <T>(), assetPath);
                    AssetDatabase.SaveAssets();

                    instance = AssetDatabase.LoadAssetAtPath <T>(assetPath);

                    if (log)
                    {
                        Debug.Log(string.Format("Generate: {0}", assetPath));
                    }
                }
            }

            return(instance);
        }
コード例 #2
0
        private static Object GenerateScriptableObject(Type type, string assetPath)
        {
            assetPath = Path.ChangeExtension(assetPath, AssetFileExtension);

            var instance = AssetDatabase.LoadAssetAtPath(assetPath, type);

            if (instance == null)
            {
                var projectPath = UnityPathUtility.ConvertProjectPath(assetPath);
                var path        = PathUtility.Combine(Application.dataPath, projectPath);

                if (!File.Exists(path))
                {
                    instance = CreateInstance(type);

                    AssetDatabase.CreateAsset(instance, assetPath);

                    AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
            }

            return(instance);
        }