Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using System;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- /// <summary>
- /// 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...
- /// </summary>
- [CreateAssetMenu(fileName = "ConnectedAsset", menuName = "Effects/ConnectedAsset", order = 1)]
- public class ConnectedAssetSO : ScriptableObject{
- //Input
- [SerializeField]
- private UnityEngine.Object baseObject;
- //Grabbed from Input
- [SerializeField]
- private Type basePathType;
- //will be created programattically but also can be edited
- [SerializeField]
- private string nameExtension;
- //Output
- [SerializeField]
- private string finalPath;
- [SerializeField]
- private UnityEngine.Object finalObject;
- public string iD = Guid.NewGuid().ToString().ToUpper();
- [SerializeField]
- private string SOname;
- public UnityEngine.Object BaseObject { get => baseObject; set => baseObject = value; }
- public Type BasePathType { get => basePathType; set => basePathType = value; }
- public string NameExtension { get => nameExtension; set => nameExtension = value; }
- public string FinalPath { get => finalPath; set => finalPath = value; }
- public UnityEngine.Object FinalObject { get => finalObject; set => finalObject = value; }
- public string SOName { get { return SOname; } set { SOname = value; } }
- public void CreateConnectedAsset(out bool doOnce) {
- //Steps for creating a new "Connected Asset"
- //We need the base path of the "donor" object -base for all copies
- var BaseConnectedAssetPath = AssetDatabase.GetAssetPath(BaseObject);
- BasePathType = BaseConnectedAssetPath.GetType();
- //we grab the parent folder - this returns the folder the base is in
- var BaseConnectedAssetParentFolder = Directory.GetParent(BaseConnectedAssetPath).FullName;
- //we grab the grandParent folder this does the same with the parent folder as the input
- var BaseConnectedAssetGrandParentFolder = Directory.GetParent(BaseConnectedAssetParentFolder).FullName;
- //We set the new file path with the file name specified
- var BaseConnectedAssetGrandParentFolderWithFileName = (BaseConnectedAssetGrandParentFolder + "/" + NameExtension + ".asset");
- //we use URI to remove unnecesary file info
- //Using Uri to convert From Absolute to relative
- var BaseConnectedAssetGrandParentFolderWithFileNameRelative = AbsoluteToRelativePath(BaseConnectedAssetGrandParentFolderWithFileName);
- //We copy the asset from the "base" path and move it to the grandparentWithFileName path
- //get unique name
- BaseConnectedAssetGrandParentFolderWithFileNameRelative = AssetDatabase.GenerateUniqueAssetPath(BaseConnectedAssetGrandParentFolderWithFileNameRelative);
- FinalPath = BaseConnectedAssetGrandParentFolderWithFileNameRelative;
- doOnce = AssetDatabase.CopyAsset(BaseConnectedAssetPath, FinalPath);
- EditorUtility.SetDirty(this);
- return;
- }
- private string AbsoluteToRelativePath(string absolutePath) {
- Uri address1 = new Uri ("file://" + Application.dataPath, UriKind.Absolute);
- Uri address2 = new Uri ("file://" + absolutePath, UriKind.Absolute);
- absolutePath = address1.MakeRelativeUri(address2).ToString();
- return absolutePath;
- }
- public void RenameConnectedAsset(string NewName) {
- var assetTypeAsType = AssetDatabase.GetMainAssetTypeAtPath(FinalPath);
- var typeToRename = AssetDatabase.LoadAssetAtPath(FinalPath,assetTypeAsType);
- typeToRename.name = NewName + NameExtension;
- AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(BaseObject), NewName + NameExtension);
- FinalPath = AssetDatabase.GetAssetPath(BaseObject);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement