unity, asset operations
2016-01-03 00:29
423 查看
//----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;
//----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;
//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;
//----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;
相关文章推荐
- 【u3d】环境搭建
- Unity-Animator深入系列---录制与回放
- 借助TinyXml,C++实现Unity的PlayerPrefs 功能
- unity与3DMax的单位问题
- Unity3D之坐标变化
- Unity3D之float Input.GetAxis(string axisname)
- Unity5脚本事件函数及其执行顺序
- Unity3D 经常使用库
- [转]How to translate WORLD coordinates to TERRAIN coordinates in Unity3d
- Unity-Animator深入系列---StateMachineBehaviour状态机脚本学习
- Unity+NGUI性能优化方法总结
- unity 2D总结(转发 忘记源地址了)
- unity4.6_摄像机跟随模式、观察模式融合(平滑)
- Unity3D教程:动画融合、动画层、动画混合、附加动画、程序动画、动画重放和取样
- Unity3D与Android,iOS交互
- 使用VS调试Unity脚本
- Unity 官方 Demo: 2DPlatformer 的 SLua 版本。
- unity中特殊文件夹
- Unity3D研究院之两种方式播放游戏视频
- Unity3d Fast Indirect illumination Using Two Virtual Spherical Gaussian Lights-Square Enix论文 ver 2