ArtToolWindow

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

[CanEditMultipleObjects]
[CustomEditor(typeof(Transform),true)]
public class ArtToolWindow : Editor {




	public    Rect                   window0 = new Rect(20, 20, 300, 150);
	private   Rect                   window1 = new Rect (325,20,150,150); 
	public    int                    toolbarOption = 0;  
	public    string[]               toolbarTexts = {"工具介绍", "粒子", "动画","一键播放"};  
	private   float                  m_CurTime_last;
	private   PlayAnimation         _PlayAnimation=new PlayAnimation();
	private   PlayAnimator          _PlayAnimator= new PlayAnimator();
	private   PlayAllParticleSystem _PlayAllParticleSystem=new PlayAllParticleSystem();
	private   GameObject            select;
	private   List<GameObject>      AnimationChilds=new List<GameObject>(); 
	private   List<GameObject>      AnimatorChilds=new List<GameObject>(); 
	public    List<GameObject>      ParticleSystemsChilds=new List<GameObject>();
	private   float                 kDuration = 0.0f;
	private   bool                  IsPause;  
	private   string                PlayOrPause="暂停粒子";
	public    enum      WindowState
	{
		None,
		Window1,

	}
	public    WindowState           _WindowState=WindowState.None;
	public string Left="L";
	
	void OnSceneGUI()
	{
		//DrawDefaultInspector ();
	
		window0 = GUI.Window(0, window0, ToolWindow, "美术小工具1.2");
		if( _WindowState==WindowState.Window1)
		  window1= GUI.Window(1, window1, CustomKey, "自定义快捷键窗口");


	}
	public override void OnInspectorGUI()
	{
		//DrawDefaultInspector ();
		Transform t = (Transform)target;
		EditorGUIUtility.LookLikeControls();
		EditorGUI.indentLevel = 0;
		Vector3 position = EditorGUILayout.Vector3Field("Position", t.localPosition);
		Vector3 eulerAngles = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
		Vector3 scale = EditorGUILayout.Vector3Field("Scale", t.localScale);
		EditorGUIUtility.LookLikeInspector();
		if (GUI.changed) {
			//SetCopyPasteBools ();
			Undo.RegisterUndo(t, "Transform Change");
			
			t.localPosition = FixIfNaN(position);
			t.localEulerAngles = FixIfNaN(eulerAngles);
			t.localScale = FixIfNaN(scale);
		}
	}

	private Vector3 FixIfNaN(Vector3 v) {
		if (float.IsNaN(v.x)) {
			v.x = 0;
		}
		if (float.IsNaN(v.y)) {
			v.y = 0;
		}
		if (float.IsNaN(v.z)) {
			v.z = 0;
		}
		return v;
	}
	//主菜单
	void ToolWindow(int windowID) 
	{

		toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarTexts); 
	    selectInit();
				switch (toolbarOption)  
				{  
				case 0:
			        ObjectShow("物体");
					break;  
				case 1:  
			        ParticleSystem("粒子");
			        break;  
				case 2:  
			        Animation("动画");
         			break;  
		        case 3:
				    PlayAll("一键播放");
			        break; 
				} 


		GUI.DragWindow ();
		
	}
	//自定义快捷键菜单
	void CustomKey(int WindowID)
	{
		GUI.DragWindow ();
		GUILayout.BeginHorizontal ();
		GUILayout.Label ("左视图");
		Left=GUILayout.TextField(Left,25);
		GUILayout.EndHorizontal ();

	}



	private void ObjectShow(string operationName)  
	{  
		GUILayout.Label("本工具正在不断的完善中      , EditorStyles.boldLabel); 
		GUILayout.BeginHorizontal();
//		if(GUILayout.Button("自定义快捷键"))
//		{
//			_WindowState=WindowState.Window1;
//		}

		if (GUILayout.Button("卸载"))
		{

			Uninstall();
		}
		EditorGUILayout.EndHorizontal();
	}





	private void Animation(string operationName)  
	{  


		if(AnimationChilds.Count!=0)
		{
		    GUILayout.Label( "所选物体的"+operationName , EditorStyles.boldLabel);  
			EditorGUILayout.BeginHorizontal();
			if (GUILayout.Button("播放动画"))
				{
					selectInit();
					 _PlayAnimation.play();
				}
				if (GUILayout.Button("停止动画"))
				{
					selectInit();
					_PlayAnimation.Stop();
				}
				EditorGUILayout.EndHorizontal();
				EditorGUILayout.BeginHorizontal();
				if (GUILayout.Button("倒播动画"))
				{
					selectInit();
					_PlayAnimation.playback();
				}
				if (GUILayout.Button("重复播放"))
				{
					selectInit();
					_PlayAnimation.repeatPlay();

				}
				EditorGUILayout.EndHorizontal();
//				if (GUILayout.Button("播放所有"))
//				{
//					_PlayAllParticleSystem.PlayParticleSystems();
//					_PlayAnimation.play();
//				}
		}

		 if (AnimatorChilds.Count != 0)
		{
			if (GUILayout.Button("播放动画(新)"))
			{
				selectInit();
				_PlayAnimator.play();
			}
			if (GUILayout.Button("停止动画(新)"))
			{
				selectInit();
				_PlayAnimator.stop();
			}
		}

		else
		{
			GUILayout.Label( "所选物体及子物体上不存在anmation组件\n或者Animator组件" , EditorStyles.boldLabel);  
		}
		
		


	}  

	private void ParticleSystem(string operationName)  
	{  

		if(ParticleSystemsChilds.Count!=0)
		{
				GUILayout.Label("所选物体的"+operationName, EditorStyles.boldLabel);  
				
				GUILayout.BeginHorizontal();  
				if (GUILayout.Button("播放粒子"))
				{
					 selectInit();
				     PlayOrPause="暂停粒子";
				     IsPause=false;
					_PlayAllParticleSystem.PlayParticleSystems();
				}
			    if (GUILayout.Button(PlayOrPause))
				{
				    IsPause=!IsPause;
					selectInit();
					if(IsPause)
						{
							PlayOrPause="继续播放";
					       _PlayAllParticleSystem.PauseParticleSystems();
			         	}
			       	else
						{
					        PlayOrPause="暂停粒子";
					        _PlayAllParticleSystem.ContinueParticleSystems();
					        
						}
				}
				GUILayout.EndHorizontal(); 
			   if (GUILayout.Button("停止粒子"))
				{
			     	 selectInit();
				     PlayOrPause="暂停粒子";
				     IsPause=false;
					_PlayAllParticleSystem.StopParticleSystems();
				}
//				if (GUILayout.Button("播放所有"))
//				{
//					_PlayAllParticleSystem.PlayParticleSystems();
//					_PlayAnimation.play();
//				}
				EditorGUILayout.Space(); 
		}
		else
		{
			GUILayout.Label( "所选物体及子物体上不存在ParticleSystem组件" , EditorStyles.boldLabel);  
		}
		
	}


	private void PlayAll(string operationName)  
	{  
		if(ParticleSystemsChilds.Count!=0&&AnimatorChilds.Count!=0||AnimationChilds.Count!=0)
		{
			if (GUILayout.Button("播放所有"))
			{
				 selectInit();
				_PlayAllParticleSystem.PlayParticleSystems();
				_PlayAnimator.play();
				_PlayAnimation.play();

			}
		}
		else
		{
			GUILayout.Label("不满足一键播放的条件\n(粒子特效和动画系统必须同时存在)", EditorStyles.boldLabel); 
		}
		
	}









	public  void ErrorShow(string Error)
	{
		EditorUtility.DisplayDialog("错误","该物体上不存在"+Error+"组件","确定");
		EditorApplication.Beep(); 
	}

	public  void ErrorAnimationClipShow(string Error )
	{
		EditorUtility.DisplayDialog("错误","模型"+Error+"里面没有挂动作","确定");
		EditorApplication.Beep(); 
	}
	//卸载改软件
	private void Uninstall()
	{
		//EditorUtility.DisplayDialog ("警告!!","确定卸载该软件","确定","取消");
		if(EditorUtility.DisplayDialog ("警告!!","确定卸载该软件","确定","取消"))
		{
			string path=Application.dataPath+"/Editor/ArtTools";
			Directory.Delete(path,true);
			AssetDatabase.Refresh();
		}


	}

	//检查所有子物体animation
	private void checkChildAnimation(GameObject select)
	{
		
		for(int i=0;i<select.transform.childCount;i++)
		{
			if(select.transform.GetChild(i).GetComponent<Animation>()!=null)
			{
				//select.transform.GetChild(i).particleSystem.Simulate(5.0f);
				AnimationChilds.Add(select.transform.GetChild(i).gameObject);
			}

			if(select.transform.GetChild(i).transform.childCount!=0)
			{
				checkChildAnimation(select.transform.GetChild(i).gameObject);
			}
			
			
		}
		
	}
	//检查所有子物体animator
	private void checkChildAnimator(GameObject select)
	{
		
		for(int i=0;i<select.transform.childCount;i++)
		{
			if(select.transform.GetChild(i).GetComponent<Animator>()!=null)
			{
				//select.transform.GetChild(i).particleSystem.Simulate(5.0f);
				AnimatorChilds.Add(select.transform.GetChild(i).gameObject);


			}
			
			if(select.transform.GetChild(i).transform.childCount!=0)
			{
				checkChildAnimator(select.transform.GetChild(i).gameObject);
			}
			
			
		}
		
	}

	//检查所有子物体particle
	private void checkParticleSystem(GameObject select)
	{
		//ParticleSystems.Clear ();
		for(int i=0;i<select.transform.childCount;i++)
		{
			if(select.transform.GetChild(i).GetComponent<ParticleSystem>()!=null)
			{
				//select.transform.GetChild(i).particleSystem.Simulate(5.0f);
				ParticleSystemsChilds.Add(select.transform.GetChild(i).gameObject);
			}
			if(select.transform.GetChild(i).transform.childCount!=0)
			{
				checkParticleSystem(select.transform.GetChild(i).gameObject);
			}
		}
	}
	//选中状态下的初始化
	private void selectInit()
	{
		AnimatorChilds.Clear ();
		AnimationChilds.Clear ();
		ParticleSystemsChilds.Clear ();
		select=Selection.activeGameObject;

		if(select.GetComponent<Animation>()!=null)
		{
			AnimationChilds.Add(select);
		}

		if(select.GetComponent<Animator>()!=null)
		{
			AnimatorChilds.Add(select);
		}
		if(select.GetComponent<ParticleSystem>()!=null)
		{
			ParticleSystemsChilds.Add(select);
		}

		checkChildAnimation(select);
		checkChildAnimator(select);
		checkParticleSystem(select);
		if (AnimationChilds.Count != 0) 
		{
		    _PlayAnimation.childs=AnimationChilds;
		}

		if (AnimatorChilds.Count!= 0) 
		{
			_PlayAnimator.childs=AnimatorChilds;
		}

		if (ParticleSystemsChilds.Count!= 0) 
		{
			_PlayAllParticleSystem.childs=ParticleSystemsChilds;
		}
	}









}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值