예제 #1
0
        /// <summary>
        /// Returns a string of the folder's path that this script is on
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetFolder(ScriptableObject obj)
        {
#if UNITY_EDITOR
            var ms   = UnityEditor.MonoScript.FromScriptableObject(obj);
            var path = UnityEditor.AssetDatabase.GetAssetPath(ms);
            var fi   = new FileInfo(path);

            var folder = fi.Directory.ToString();
            folder = folder.Replace('\\', '/');
            return(StratusIO.MakeRelative(folder));
#else
            return(string.Empty);
#endif
        }
        /// <summary>
        /// Loads the scriptable object from an Unity relative path.
        /// Returns null if the object doesn't exist.
        /// </summary>
        /// <typeparam name="T">The type of ScriptableObject</typeparam>
        /// <param name="path">The relative path to the asset. (e.g: "Assets/Resources/MyFile.asset")</param>
        /// <returns>The saved data as a ScriptableObject.</returns>
        public static T LoadScriptableObject <T>(string path) where T : ScriptableObject
        {
            var resourcesFolder = string.Concat(UnityDirectorySeparator, ResourcesFolderName, UnityDirectorySeparator);

            if (!path.Contains(resourcesFolder))
            {
                var exceptionMessage = string.Format(
                    "Failed to load ScriptableObject of type, {0}, from path: {1}. " +
                    "Path must begin with Assets and include a directory within the Resources folder.",
                    typeof(T).ToString(),
                    path);
                throw new UnityException(exceptionMessage);
            }

            // Mkae sure we are using a relative path
            var resourceRelativePath = StratusIO.MakeRelative(path);
            // Remove the file extension
            var fileExtension = Path.GetExtension(path);

            resourceRelativePath = resourceRelativePath.Replace(fileExtension, string.Empty);
            //Trace.Script("Loading data from " + resourceRelativePath);
            return(Resources.Load <T>(resourceRelativePath));
        }