예제 #1
0
 void BasicAssetOut.PickAssetAtPath <ElementType>(string path, Taker <ElementType> collector)
 {
     try {
         var contentFullPath = Path.GetDirectoryName(assetUnitInfo.localref) + "/" + path;
         if (typeof(ElementType) == (typeof(ModelImporter)))
         {
             var importer = (ElementType)(object)ModelImporter.GetAtPath(contentFullPath);
             collector.Take(importer);
         }
         else if (typeof(UnityEngine.Object).IsAssignableFrom(typeof(ElementType)))
         {
             try {
                 foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(contentFullPath))
                 {
                     try {
                         var concreteAsset = (ElementType)(object)asset;
                         if (concreteAsset != null)
                         {
                             collector.Take(concreteAsset);
                             break;
                         }
                     } catch { }
                 }
             } catch {
                 collector.OnFail("StdEditTimeUnityAssetIO.PickAssetAtPath: ");
             }
         }
         else
         {
             if (File.Exists(contentFullPath))
             {
                 collector.Take(RequiredFuncs.FromJsonAtPath <ElementType>(contentFullPath));
             }
         }
     } catch (Exception e) {
         Debug.LogError(e);
     }
     collector.OnFinish();
 }
예제 #2
0
 AssetAddResult <AssetContentType> BasicAssetIn.SetContent <AssetContentType>(BasicAssetInParam <AssetContentType> settingParam)
 {
     try {
         var assetRootPath = Path.GetDirectoryName(assetUnitInfo.localref);
         if (!Directory.Exists(assetRootPath))
         {
             Directory.CreateDirectory(assetRootPath);
         }
         var assetPath = assetRootPath + "/" + settingParam.assetPath;
         if (typeof(AssetContentType) == typeof(GameObject))
         {
             var        gObj       = (GameObject)Convert.ChangeType(settingParam.content, typeof(GameObject));
             GameObject oldPrefab  = AssetDatabase.LoadAssetAtPath <GameObject>(assetPath);
             GameObject newContent = null;
             if (oldPrefab != null && settingParam.doOverwrite)
             {
                 newContent = PrefabUtility.ReplacePrefab(gObj, oldPrefab);
             }
             else
             {
                 newContent = PrefabUtility.CreatePrefab(assetPath, gObj);
             }
             EditorUtility.SetDirty(newContent);
             AssetDatabase.SaveAssets();
             return(new AssetAddResult <AssetContentType> {
                 permanentContent = (AssetContentType)Convert.ChangeType(newContent, typeof(AssetContentType)), didSuccess = true
             });
         }
         else if (typeof(UnityEngine.Object).IsAssignableFrom(typeof(AssetContentType)))
         {
             UnityEngine.Object obj = (UnityEngine.Object)(object) settingParam.content;
             var oldAssetPath       = AssetDatabase.GetAssetPath(obj);
             if (Utilities.IsStringEmpty(oldAssetPath))
             {
                 AssetDatabase.CreateAsset(obj, assetPath);
             }
             else
             {
                 if (oldAssetPath.StartsWith("Assets/"))
                 {
                     AssetDatabase.MoveAsset(oldAssetPath, assetPath);
                 }
                 else
                 {
                     if (typeof(AssetContentType) == typeof(Material))
                     {
                         AssetDatabase.CreateAsset(new Material((Material)(object)obj), assetPath);
                     }
                 }
             }
             EditorUtility.SetDirty(obj);
             AssetDatabase.SaveAssets();
             return(new AssetAddResult <AssetContentType> {
                 permanentContent = settingParam.content, didSuccess = true
             });
         }
         else if (typeof(CommonAssetRef).IsAssignableFrom(typeof(AssetContentType)))
         {
             if (File.Exists(assetPath) && settingParam.doOverwrite)
             {
                 try {
                     return(new AssetAddResult <AssetContentType> {
                         didSuccess = false, permanentContent = RequiredFuncs.FromJsonAtPath <AssetContentType>(assetPath)
                     });
                 } catch { return(new AssetAddResult <AssetContentType> {
                         didSuccess = false
                     }); }
             }
             File.WriteAllText(assetPath, RequiredFuncs.ToJson(settingParam.content));
             //AssetDatabase.Refresh();
             //AssetDatabase.SaveAssets();
             return(new AssetAddResult <AssetContentType> {
                 permanentContent = settingParam.content, didSuccess = true
             });
         }
     } catch (Exception e) { Debug.LogError(e); }
     return(new AssetAddResult <AssetContentType> {
         didSuccess = false
     });
 }