Advertisement
jamieTheCoder

ConnectedAsset

Jan 24th, 2023 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | Software | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9. /// <summary>
  10. /// I have no idea how to get the data from the constructor to CreateConnectedAssets
  11. /// </summary>
  12. [System.Serializable]
  13. public class ConnectedAsset {
  14.     public UnityEngine.Object ConnectedAssetObject;
  15.     public string AssetPath;
  16.  
  17.     public ConnectedAsset(string BasePath, string ObjName) {
  18.         //We have to save the data In a struct Because We can't pass it into the delegate
  19.         //now that the input data is saved we can Save the reference to the new ConnectedAsset so we can access it later
  20.     }
  21.     //This should work but I need to pass data into this
  22.     [UnityEditor.Callbacks.DidReloadScripts]
  23.     private static void CreateAssetWhenReady() {
  24.         //need to get the insides to only call after loading when constructor is called...
  25.  
  26.         if (EditorApplication.isCompiling || EditorApplication.isUpdating) {
  27.             EditorApplication.delayCall += CreateAssetWhenReady;
  28.             return;
  29.         }
  30.  
  31.         EditorApplication.delayCall += CreateAssetNow;
  32.     }
  33.     public static void CreateAssetNow() {
  34.     }
  35.  
  36.     public void CreateConnectedAsset(string BasePath, string ObjName, out string finalPath,out UnityEngine.Object Obj) {
  37.         //Steps for creating a new "Connected Asset"
  38.         //We need the base path of the "donor" object -base for all copies
  39.         var BaseConnectedAssetPath = BasePath;
  40.         //we grab the parent folder - this returns the folder the base is in
  41.         var BaseConnectedAssetParentFolder = Directory.GetParent(BaseConnectedAssetPath).FullName;
  42.         //we grab the grandParent folder this does the same with the parent folder as the input
  43.         var BaseConnectedAssetGrandParentFolder = Directory.GetParent(BaseConnectedAssetParentFolder).FullName;
  44.         //We set the new file path with the file name specified
  45.         var BaseConnectedAssetGrandParentFolderWithFileName = (BaseConnectedAssetGrandParentFolder + "/" + ObjName + ".asset");
  46.         //we use URI to remove unnecesary file info
  47.         //Using Uri to convert From Absolute to relative
  48.         var BaseConnectedAssetGrandParentFolderWithFileNameRelative = AbsoluteToRelativePath(BaseConnectedAssetGrandParentFolderWithFileName);
  49.         //We copy the asset from the "base" path and move it to the grandparentWithFileName path
  50.         //get unique name
  51.         BaseConnectedAssetGrandParentFolderWithFileNameRelative = AssetDatabase.GenerateUniqueAssetPath(BaseConnectedAssetGrandParentFolderWithFileNameRelative);
  52.         finalPath = BaseConnectedAssetGrandParentFolderWithFileNameRelative;
  53.         AssetDatabase.CopyAsset(BaseConnectedAssetPath, BaseConnectedAssetGrandParentFolderWithFileNameRelative);
  54.         Obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(finalPath);
  55.         return;
  56.     }
  57.     private string AbsoluteToRelativePath(string absolutePath) {
  58.         Uri address1 = new Uri("file://" + Application.dataPath, UriKind.Absolute);
  59.         Uri address2 = new Uri("file://" + absolutePath, UriKind.Absolute);
  60.         absolutePath = address1.MakeRelativeUri(address2).ToString();
  61.         return absolutePath;
  62.     }
  63.     //private void RenameFloatCollection(EffectSharedData tempEffectData) {
  64.     //    if (tempEffectData.Floats.name == tempEffectData.name + "FloatCollection") {
  65.     //        return;
  66.     //    }
  67.     //    FloatCollection floatCollection = (FloatCollection)AssetDatabase.LoadAssetAtPath(FloatsPath, typeof(FloatCollection));
  68.     //    floatCollection.name = tempEffectData.name + "FloatCollection";
  69.     //    AssetDatabase.RenameAsset(FloatsPath, tempEffectData.name + "FloatCollection.asset");
  70.     //    FloatsPath = AssetDatabase.GetAssetPath(Floats);
  71.     //    return;
  72.     //}
  73.     ////probably need to change function name  OnDestroy Might be called unintentionally...
  74.     //public void OnDestroy() {
  75.     //    var IntPath = AssetDatabase.GetAssetPath(Ints);
  76.     //    var FloatPath = AssetDatabase.GetAssetPath(Floats);
  77.     //    AssetDatabase.DeleteAsset(FeedbackPrefabPath);
  78.     //    AssetDatabase.DeleteAsset(IntPath);
  79.     //    AssetDatabase.DeleteAsset(FloatPath);
  80.     //}
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement