一键填充数据

需求分析

宠物预设上绑定脚本,需要填充很多数据,创建材质球,主要功能为:
1.填充Animaiton、材质球、贴图等数据
2.保存预设
3.极简,并只在编辑器下有效

一键填充数据_第1张图片
企业微信截图_15215497412251.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(GamePetVisual),true)]
public class PetPrefabTool : UIWidgetContainerEditor
{
    const string NormalPath = "Assets/IDS/Res/Models/Pet";
    public string AnimationRootPath = "";

    public Animation animddd;

    public Texture MainTexture = null;

    public override void OnInspectorGUI()
    {
        MainTexture = EditorGUILayout.ObjectField("添加主贴图", MainTexture, typeof(Texture), true) as Texture;
        if(MainTexture!=null)
        {
            if(string.IsNullOrEmpty(AnimationRootPath))
            {
                string mainTexturePath = AssetDatabase.GetAssetPath(MainTexture.GetInstanceID());

                AnimationRootPath = mainTexturePath.Replace(MainTexture.name+".png","");

                GamePetVisual gamePetVisual = (GamePetVisual)target;
                gamePetVisual.MainTexture = MainTexture;
            }
        }
        else
        {
            GamePetVisual gamePetVisual = (GamePetVisual)target;
            if(gamePetVisual.MainTexture != null)
            {
                MainTexture = gamePetVisual.MainTexture;
            }            
        }
        AnimationRootPath = EditorGUILayout.TextField("动画文件路径:", AnimationRootPath);

        if (GUILayout.Button("一键填充"))
        {
            if(string.IsNullOrEmpty(AnimationRootPath))
            {
                EditorUtility.DisplayDialog("错误", "动画文件路径不能为空,必须先指定路径!", "是");
                return;
            }

            if (MainTexture == null)
            {
                EditorUtility.DisplayDialog("错误", "主贴图名不能为空,必须先设置!例如:xiaoshumiao", "是");
                return;
            }

            GamePetVisual gamePetVisual = (GamePetVisual)target;
            setPetAimiation(gamePetVisual);
            setGamePetVisual(gamePetVisual);

            NGUITools.SetLayer(gamePetVisual.gameObject, LayerMask.NameToLayer("Dude"));
            gamePetVisual.MainTexture = MainTexture;
            EditorUtility.SetDirty(gamePetVisual.gameObject);
            AssetDatabase.SaveAssets();
        }

        base.OnInspectorGUI();
    }

    void setPetAimiation(GamePetVisual gamePetVisual)
    {
        string[] animsName = {"deathRole","idle","flightIdle","glide","flight"};

        List clips = new List();
        for (int i = 0; i < animsName.Length; i++)
        {
            string animPath = AnimationRootPath + animsName[i]+".anim";
            AnimationClip animClip = AssetDatabase.LoadAssetAtPath(animPath);
            if(animClip!=null)
            {
                clips.Add(animClip);
                Debug.Log("Find Clips :" + animPath);
            }
        }

        if(clips.Count>0)
        {
            Animation myAnimation = gamePetVisual.gameObject.AddMissingComponent();

            AnimationUtility.SetAnimationClips(myAnimation,clips.ToArray());
            myAnimation.playAutomatically = true;
            myAnimation.animatePhysics = false;
            myAnimation.cullingType = AnimationCullingType.BasedOnRenderers;

            myAnimation.clip = clips[0];
        }
    }


    void setGamePetVisual(GamePetVisual gamePetVisual)
    {

        gamePetVisual.animatorType = AnimatorType.Eagle;

        SkinnedMeshRenderer render = gamePetVisual.GetComponentInChildren();
        if(render!=null)
        {
            gamePetVisual.meshRenderer = render;
        }

        string transMaterialPaht = string.Format("{0}Materials/{1}_transparent.mat", AnimationRootPath, MainTexture.name);
        Material transMaterial = AssetDatabase.LoadAssetAtPath(transMaterialPaht);
        if(transMaterial==null)
        {
            //创建一个新材质球
            Shader shader = Shader.Find("TempleRun2/Additive");
            if(shader==null)
            {
                EditorUtility.DisplayDialog("错误", "Shader TempleRun2/Additive is null,检查为什么!", "是");
                return;
            }
            transMaterial = new Material(shader);
            transMaterial.color = Color.white;
            
            AssetDatabase.CreateAsset(transMaterial, transMaterialPaht);
        }
        transMaterial = AssetDatabase.LoadAssetAtPath(transMaterialPaht);
        if(transMaterial!=null)
        {
            if (MainTexture != null)
            {
                transMaterial.mainTexture = MainTexture;
            }
            else
            {
                Debug.LogError("MainTexture is null !");
            }
            gamePetVisual.transparentMaterial = transMaterial;
        }

    }
}

你可能感兴趣的:(一键填充数据)