zoukankan      html  css  js  c++  java
  • unity, asset operations

    //----create asset

    //ref: http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset

    CmyScriptableObject asset = ScriptableObject.CreateInstance<CmyScriptableObject> ();
     
            AssetDatabase.CreateAsset (asset, path);

            AssetDatabase.SaveAssets ();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow ();
            Selection.activeObject = asset;


            //注:CmyScriptableObject类型最好单独定义在CmyScriptableObject.cs文件里,以保证所生成的asset文件的Script成员为非空。

    //----create a scriptable object and add it to an existing asset
                        CmyScriptableObject obj = ScriptableObject.CreateInstance<CmyScriptableObject> ();
                        AssetDatabase.AddObjectToAsset(obj,existingAsset);

                        // Reimport the asset after adding an object.
                        // Otherwise the change only shows up when saving the project
                        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(obj));


                        Selection.activeObject = obj;

    //----remove object in asset

    //ref : http://answers.unity3d.com/questions/219465/how-can-i-remove-an-object-from-an-asset.html

                        UnityEngine.Object.DestroyImmediate(obj, true);

                //save

                              AssetDatabase.SaveAssets();

    //----get subasset of certain class type

    CmyScriptableObject obj = AssetDatabase.LoadAssetAtPath<CmyScriptableObject> (path);
           

    //----get all objects(sub assets) from assets

    //ref: http://answers.unity3d.com/questions/1066162/how-do-i-return-the-path-of-a-sub-asset-in-an-asse.html

    //suppose asset have objects(sub assets) attached to it

    UnityEngine.Object[] allassets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(asset));
                        int assetCount=allassets.Length;
                        Debug.Log("assetCount:"+assetCount);
                        for(int i=0;i<assetCount;i++){
                            UnityEngine.Object _asset=allassets[i];
                            Debug.Log(_asset.name);
                        }

    //----save asset

    //if only want save the specified asset, use:

    EditorUtility.SetDirty (asset);

    //if want save all assets, use:

    AssetDatabase.SaveAssets ();

    //----rename asset

    string errorMessage=AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(asset),"new name");
                if(errorMessage.Length==0){
                    Debug.Log("rename asset succ");
                }else{
                    Debug.Log("rename asset failed: "+errorMessage);
                }

    //----rename subasset

    CmyScriptableObject subAsset = AssetDatabase.LoadAssetAtPath<CmyScriptableObject> (AssetDatabase.GetAssetPath(asset));
            subAsset.name = "new name";

    //----hide subasset in Assets folder

    //ref: http://answers.unity3d.com/questions/210726/how-to-hide-files-in-project-directory.html

    CmyScriptableObject subAsset = AssetDatabase.LoadAssetAtPath<CmyScriptableObject> (AssetDatabase.GetAssetPath(asset));
      subAsset.hideFlags = HideFlags.HideInHierarchy;

  • 相关阅读:
    Android自己定义组件系列【2】——Scroller类
    ostringstream的使用方法
    什么是Spring?Spring是什么?
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    伤不起的戴尔台式机XPS8700脆弱的蓝牙
    cocos2d-x-3.1 事件分发机制 (coco2d-x 学习笔记七)
    SSL工作原理
    AfxMessageBox和MessageBox差别
    oninput,onpropertychange,onchange的使用方法和差别
    Bootstrap网站模板
  • 原文地址:https://www.cnblogs.com/wantnon/p/5095651.html
Copyright © 2011-2022 走看看