Advertisement
jamieTheCoder

ConnectedAssetSO

Jan 30th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 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. /// This script is meant to create any object when connectedAssetScriptableObject is made (basically to make sure you have the data you need and nothing more while still being modular and extendable...
  11. /// </summary>
  12. [CreateAssetMenu(fileName = "ConnectedAsset", menuName = "Effects/ConnectedAsset", order = 1)]
  13. public class ConnectedAssetSO : ScriptableObject{
  14.     //Input
  15.     [SerializeField]
  16.     private UnityEngine.Object baseObject;
  17.     //Grabbed from Input
  18.     [SerializeField]
  19.     private Type basePathType;
  20.     //will be created programattically but also can be edited
  21.     [SerializeField]
  22.     private string nameExtension;
  23.     //Output
  24.     [SerializeField]
  25.     private string finalPath;
  26.     [SerializeField]
  27.     private UnityEngine.Object finalObject;
  28.  
  29.     public string iD = Guid.NewGuid().ToString().ToUpper();
  30.     [SerializeField]
  31.     private string SOname;
  32.  
  33.     public UnityEngine.Object BaseObject { get => baseObject; set => baseObject = value; }
  34.     public Type BasePathType { get => basePathType; set => basePathType = value; }
  35.     public string NameExtension { get => nameExtension; set => nameExtension = value; }
  36.     public string FinalPath { get => finalPath; set => finalPath = value; }
  37.     public UnityEngine.Object FinalObject { get => finalObject; set => finalObject = value; }
  38.     public string SOName { get { return SOname; } set { SOname = value; } }
  39.  
  40.     public void CreateConnectedAsset(out bool doOnce) {
  41.         //Steps for creating a new "Connected Asset"
  42.         //We need the base path of the "donor" object -base for all copies
  43.         var BaseConnectedAssetPath = AssetDatabase.GetAssetPath(BaseObject);
  44.         BasePathType = BaseConnectedAssetPath.GetType();
  45.         //we grab the parent folder - this returns the folder the base is in
  46.         var BaseConnectedAssetParentFolder = Directory.GetParent(BaseConnectedAssetPath).FullName;
  47.         //we grab the grandParent folder this does the same with the parent folder as the input
  48.         var BaseConnectedAssetGrandParentFolder = Directory.GetParent(BaseConnectedAssetParentFolder).FullName;
  49.         //We set the new file path with the file name specified
  50.         var BaseConnectedAssetGrandParentFolderWithFileName = (BaseConnectedAssetGrandParentFolder + "/" + NameExtension + ".asset");
  51.         //we use URI to remove unnecesary file info
  52.         //Using Uri to convert From Absolute to relative
  53.         var BaseConnectedAssetGrandParentFolderWithFileNameRelative = AbsoluteToRelativePath(BaseConnectedAssetGrandParentFolderWithFileName);
  54.         //We copy the asset from the "base" path and move it to the grandparentWithFileName path
  55.         //get unique name
  56.         BaseConnectedAssetGrandParentFolderWithFileNameRelative = AssetDatabase.GenerateUniqueAssetPath(BaseConnectedAssetGrandParentFolderWithFileNameRelative);
  57.         FinalPath = BaseConnectedAssetGrandParentFolderWithFileNameRelative;
  58.         doOnce = AssetDatabase.CopyAsset(BaseConnectedAssetPath, FinalPath);
  59.         EditorUtility.SetDirty(this);
  60.         return;
  61.     }
  62.     private string AbsoluteToRelativePath(string absolutePath) {
  63.         Uri address1 = new Uri ("file://" + Application.dataPath, UriKind.Absolute);
  64.         Uri address2 = new Uri ("file://" + absolutePath, UriKind.Absolute);
  65.         absolutePath = address1.MakeRelativeUri(address2).ToString();
  66.         return absolutePath;
  67.     }
  68.     public void RenameConnectedAsset(string NewName) {
  69.         var assetTypeAsType = AssetDatabase.GetMainAssetTypeAtPath(FinalPath);
  70.         var typeToRename = AssetDatabase.LoadAssetAtPath(FinalPath,assetTypeAsType);
  71.         typeToRename.name = NewName + NameExtension;
  72.         AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(BaseObject), NewName + NameExtension);
  73.         FinalPath = AssetDatabase.GetAssetPath(BaseObject);
  74.         return;
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement