unity 工具开发基础

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Unity基础工具功能
/// priority菜单项显示排序
/// </summary>
public class TestEditor : MonoBehaviour {

    [MenuItem("GameObject/获取选中的物体名称", priority = 0)]
    static string GetSelectObject(){
        GameObject _selectObject = Selection.gameObjects [0];
        Debug.Log (_selectObject.name);
        return _selectObject.name;
    }

    [MenuItem("GameObject/获取Project视图中的文件和文件夹路径", priority = 1)]
    static string GetSelectFile(){
        string[] guidArray = Selection.assetGUIDs;//从meta文件里面取得id
        string selectName = AssetDatabase.GUIDToAssetPath (guidArray [0]);//根据id取得路径
        string path = Application.dataPath.Substring (0, Application.dataPath.Length - 6);
        Debug.Log (path + selectName);
        return path + selectName;
    }

    [MenuItem("GameObject/创建新文件", priority = 0)]
    static void CreateNewFile(){
        File.WriteAllText (GetSelectFile () + "/UI_" + GetSelectObject () + ".lua", "xxx文本内容xxx", Encoding.UTF8);
        AssetDatabase.Refresh ();
    }

    [MenuItem("GameObject/创建文件夹", priority = 0)]
    static void CreateFolder(){
        Directory.CreateDirectory(Application.dataPath + "/文件夹名称");
        AssetDatabase.Refresh ();
    }
}

放Editor文件下

 

editor修改prefab    https://cnblogs.com/klkucan/p/4934518.html

 

-10-23新增

 

    [MenuItem("GameObject/获取用户名和当前时间", priority = 0)]
    static void GetUserName(){
        string username = System.Environment.UserName;
        var time = System.DateTime.Now;
        Debug.Log (username);
        Debug.Log (time);
    }

    [MenuItem("GameObject/打印选择物体的子物体路径", priority = 0)]
    static void GetChildPath()
    {
        Transform _selectObject = Selection.gameObjects[0].transform;
        GetChildObject (_selectObject, _selectObject.name);
    }

    static void GetChildObject(Transform trans, string path){
        for(int i = 0; i < trans.childCount; i++){
            Transform child = trans.GetChild (i);
            string comPath = path + "_" + child.name;
            GetChildObject (child, comPath);
        }
        //输出条件:最末端物体 (自由更改)
        if(trans.childCount == 0){
            Debug.Log (path);
        }
    }

 打印 路径 和 名字

    static void CreateUI(){
        string[] guidArray = Selection.assetGUIDs;
        foreach (var item in guidArray)
        {
            //打印路径
            string selecetFloder = AssetDatabase.GUIDToAssetPath(item);
            Debug.Log(selecetFloder);
            //打印名字
            string selectName = Path.GetFileName(selecetFloder);
            Debug.Log(selectName);
        }
    }

 

 

遍历文件夹下的包括子文件夹的后缀带有xx文件

以lua为例

    [MenuItem("Assets/check",priority = 1)]
    static void CheckChinese()
    {
        string[] guidArray = Selection.assetGUIDs;
        foreach (var item in guidArray)
        {
            string selectFloder = AssetDatabase.GUIDToAssetPath(item);
            DirectoryInfo root = new DirectoryInfo(selectFloder);
            GetFloder(root);
        }
    }

    static void GetFloder(DirectoryInfo root)
    {
        GetFile(root);
        //查找子文件夹
        DirectoryInfo[] array = root.GetDirectories();
        //Debug.Log(root);
        foreach (DirectoryInfo item in array)
        {
            GetFloder(item);
        }
    }

    static void GetFile(DirectoryInfo root)
    {
        //DirectoryInfo root = new DirectoryInfo(path);
        FileInfo[] fileDic = root.GetFiles();
        foreach (var file in fileDic)
        {
            //sDebug.Log(file);
            if (file.FullName.EndsWith("lua"))
            {
                Debug.Log("-------------" + file.Name);
            }
        }
    }

 

//获取指定类型资源的路径
string[] spritesPath =
AssetDatabase.FindAssets("t:Sprite", new string[] {Application.dataPath + "/Resources/Sprites/Cloud"});


--------------------------------2019-8-28------------------------------------
代码设置Animator的参数,animator 转 AnimatorController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Spine.Unity;
using UnityEditor.Animations;

/// <summary>
/// 制作怪物预制体
/// </summary>
public class CreateMonsterPrefab {

    [MenuItem("GameObject/添加怪物Animator参数", priority = 0)]
    static void SetACPara()
    {
        Debug.Log("设置参数");
        GameObject _selectObject = Selection.gameObjects[0];
        AnimatorController controller = _selectObject.GetComponent<Animator>().runtimeAnimatorController as AnimatorController;

        AddParameter(controller, "Die", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Move", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Hurt", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "IsDead", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Speed", AnimatorControllerParameterType.Float);
        AddParameter(controller, "Vertigo", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Execute", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "IsMoveModeEnable", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Frozen", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Warning", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "IsWarning", AnimatorControllerParameterType.Trigger);
        AddParameter(controller, "IsAtk", AnimatorControllerParameterType.Bool);
        AddParameter(controller, "Attack", AnimatorControllerParameterType.Int);
        AddParameter(controller, "AtkType", AnimatorControllerParameterType.Int);
        AddParameter(controller, "MoveType", AnimatorControllerParameterType.Int);
        AddParameter(controller, "AttackType", AnimatorControllerParameterType.Int);

        Debug.Log("设置成功");
    }

    //添加
    static void AddParameter(AnimatorController controller, string name, AnimatorControllerParameterType _type)
    {
        if (!CheckParameters(controller, name))
        {
            controller.AddParameter(name, _type);
        }
    }

    //检查
    static bool CheckParameters(AnimatorController controller, string parName)
    {
        foreach (var item in controller.parameters)
        {
            if (item.name.Equals(parName))
            {
                return true;
            }
        }
        return false;
    }
}

 

不错的教程:
https://blog.csdn.net/q764424567/article/details/80908614



posted @ 2018-10-22 10:01  三页菌  阅读(1019)  评论(0编辑  收藏  举报