コード例 #1
0
    public static void CreateAsset(ScriptableObject o, string name)
    {
        string path;
        if (o.GetType() == typeof(Quest))
        {
            path = "Assets/Resources/Quests";
        }
        else
            path = "Assets/Resources/Items";

        if (Path.GetExtension(path) != "")
        {
            path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), "");
        }

        string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + name + ".asset");
        if (!AssetDatabase.Contains(o))
        {
            AssetDatabase.CreateAsset(o, assetPathAndName);
            Debug.Log("Asset has been created in " + assetPathAndName);
        }
        else
        {
            Debug.Log("Asset has already been in database in " + o.ToString());
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = o;
            return;
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = o;
    }
コード例 #2
0
        /// <summary>
        //    This makes it easy to create, name and place unique new ScriptableObject asset files.
        /// </summary>
        public static void CreateAssetAtPath( ScriptableObject pxObject, string pxPath, string pxFilename )
        {
            string pxFullPath = "";

            string pxAssetPath = "Assets";
            string pxExtension = ".asset";

            bool bPathSupplied = !string.IsNullOrEmpty( pxPath );
            bool bFilenameSupplied  = !string.IsNullOrEmpty( pxFilename );

            // Concatenate the full path for the asset
            if ( bPathSupplied )
            {
                List<string> pxPathComponents = new List<string>();

                bool bAssetPathSupplied = pxPath.StartsWith( pxAssetPath );
                if ( !bAssetPathSupplied )
                {
                    pxPathComponents.Add( pxAssetPath );
                    pxPathComponents.Add( "/" );
                }

                pxPathComponents.Add( pxPath );

                bool bPathHasTrailingSlash = pxPath.EndsWith( "/" );
                if ( !bPathHasTrailingSlash )
                {
                    pxPathComponents.Add( "/" );
                }

                if ( bFilenameSupplied )
                {
                    pxPathComponents.Add( pxFilename );

                    bool bExtensionSupplied = pxFilename.EndsWith( pxExtension );
                    if ( !bExtensionSupplied )
                    {
                        pxPathComponents.Add( pxExtension );
                    }
                }
                else
                {
                    pxPathComponents.Add( pxObject.name );
                    pxPathComponents.Add( pxExtension );
                }

                string[] pxComponentArray = pxPathComponents.ToArray();
                pxFullPath = string.Concat( pxComponentArray );
            }
            else
            {
                string pxObjectTypeName = pxObject.GetType().Name;
                pxFullPath = pxAssetPath + "/" + pxObjectTypeName + pxExtension;
            }

            // Create the Asset
            Debug.Log( "Creating asset at: " + pxFullPath + "..." );
            AssetDatabase.CreateAsset( pxObject, pxFullPath );
            AssetDatabase.SaveAssets();
        }
コード例 #3
0
    public static T GetScriptableObjectInterface <T>(this UnityEngine.ScriptableObject o)
    {
        var targetType = typeof(T);
        var objectType = o.GetType();

        foreach (var objectInterface in objectType.GetInterfaces())
        {
            if (objectInterface == targetType)
            {
                return(o.To <T>());
            }
        }

        return(default(T));
    }
コード例 #4
0
    public static List <T> GetScriptableObjectInterfaces <T>(this UnityEngine.ScriptableObject o)
    {
        var result     = new List <T>();
        var targetType = typeof(T);
        var objectType = o.GetType();

        foreach (var objectInterface in objectType.GetInterfaces())
        {
            if (objectInterface == targetType)
            {
                result.Add(o.To <T>());
            }
        }

        return(result);
    }
 public static void Error(this ScriptableObject scriptableObject, string debugMessage,
                          [System.Runtime.CompilerServices.CallerMemberName] string methodName = default)
 {
     Debug.LogError(string.Format("{0}.{1}: {2}", scriptableObject.GetType().Name, methodName, debugMessage));
 }
 public static void Temp(this ScriptableObject scriptableObject, string debugMessage,
                         [System.Runtime.CompilerServices.CallerMemberName] string methodName = default)
 {
     Debug.Log(string.Format("<color=green>{0}.{1}: {2}</color>",
                             scriptableObject.GetType().Name, methodName, debugMessage));
 }