1. [cpp] 
  2. using UnityEngine;
  3. using System.Collections;
  4. using Holoville.HOTween;
  5. //[RequireComponent(typeof(BoxCollider))]
  6. public class VKButton : VKView {
  7. [HideInInspector] public Material buttonDefultMat;
  8. [HideInInspector] public Mesh buttonDefultMesh;
  9. [HideInInspector] public Texture buttonTex,pressButtonTex;
  10. [HideInInspector] public string pressEventName = null;
  11. [HideInInspector] public string clickEventName = null;
  12. [HideInInspector] public string dragStartEventName = null;
  13. [HideInInspector] public string dragEventName = null;
  14. [HideInInspector] public string dragEndEventName = null;
  15. [HideInInspector] public GameObject eventObj = null;
  16. [HideInInspector] public string eventScript = null;
  17. [HideInInspector] public bool isDrag = false;
  18. [HideInInspector] public float scale = 1;
  19. [HideInInspector] public string info= null;
  20. [HideInInspector] public string[] animatorType = new string[]{"null","bigger","smaller","moveLeft","moveRight"};
  21. [HideInInspector] public int currentAnimator = 0;
  22. [HideInInspector] public bool isAni = false;
  23. void Start () {
  24. buttonDefultMat = new Material(Shader.Find("VK/VKButtonShader"));
  25. gameObject.GetComponent<MeshRenderer> ().material = buttonDefultMat;
  26. buttonDefultMesh = new Mesh ();
  27. gameObject.GetComponent<MeshFilter> ().mesh = buttonDefultMesh;
  28. if(GetComponent<BoxCollider>()== null)
  29. gameObject.AddComponent<BoxCollider>();
  30. updateButton();
  31. }
  32. public float setScale{
  33. set{
  34. scale = value;
  35. }get{
  36. return scale;
  37. }
  38. }
  39. public void updateButton(){
  40. if(buttonTex!=null){
  41. if(buttonDefultMat!=null)
  42. buttonDefultMat.mainTexture = buttonTex;
  43. if(buttonDefultMesh!=null)
  44. buttonDefultMesh.vertices = InitBase.initVertice(buttonTex.width *scale,buttonTex.height*scale,ancPointx,ancPointy);
  45. if(this!=null&&gameObject.GetComponent<BoxCollider>()!=null){
  46. gameObject.GetComponent<BoxCollider>().size = new Vector3(buttonTex.width*scale,buttonTex.height*scale,0);
  47. }
  48. }else{
  49. if(buttonDefultMat!=null)
  50. buttonDefultMat.mainTexture = null;
  51. if(buttonDefultMesh!=null)
  52. buttonDefultMesh.vertices = InitBase.initVertice(width*scale,height*scale,ancPointx,ancPointy);
  53. gameObject.GetComponent<BoxCollider>().size = new Vector3(width,height,0);
  54. }
  55. if(buttonDefultMesh!= null){
  56. buttonDefultMesh.triangles = InitBase.initTri ();
  57. buttonDefultMesh.normals = InitBase.initNormal();
  58. buttonDefultMesh.uv = InitBase.initUV();
  59. }
  60. }
  61. Vector3 pressScale= Vector3.zero;
  62. Vector3 pressPos = Vector3.zero;
  63. Tweener tw = null;
  64. void onPressAni(){
  65. if(tw!=null && !tw.isComplete){
  66. return;
  67. }
  68. pressScale = transform.localScale;
  69. pressPos = transform.position;
  70. switch(currentAnimator){
  71. case 1:
  72. tw = VKAnimator.scaleBy(gameObject,0.3f,Vector3.one*0.2f,VkAniDuration.AnimationCurve,null);
  73. break;
  74. case 2:
  75. tw = VKAnimator.scaleBy(gameObject,0.3f,Vector3.one*-0.2f,VkAniDuration.AnimationCurve,null);
  76. break;
  77. case 3:
  78. tw = VKAnimator.moveBy(gameObject,0.3f,new Vector3(-10f,0,0),false,VkAniDuration.AnimationCurve,null);
  79. break;
  80. case 4:
  81. tw = VKAnimator.moveBy(gameObject,0.3f,new Vector3(10f,0,0),false,VkAniDuration.AnimationCurve,null);
  82. break;
  83. }
  84. }
  85. void onClickAni(){
  86. if(currentAnimator == 1 || currentAnimator==2){
  87. VKAnimator.scaleTo(gameObject,0.3f,pressScale,VkAniDuration.AnimationCurve,null);
  88. } else if(currentAnimator == 3 || currentAnimator==4){
  89. VKAnimator.moveTo(gameObject,0.3f,pressPos,false,VkAniDuration.AnimationCurve,null);
  90. }
  91. }
  92. public void switchImageView(){
  93. VKImageView imgView = gameObject.AddComponent<VKImageView> ();
  94. imgView.imgViewTex = buttonTex;
  95. imgView.highLightedTex = pressButtonTex;
  96. imgView.ancPointx = ancPointx;
  97. imgView.ancPointy = ancPointy;
  98. imgView.scale = scale;
  99. imgView.updateImageView ();
  100. if(GetComponent<BoxCollider>()){
  101. DestroyImmediate(GetComponent<BoxCollider>());
  102. }
  103. DestroyImmediate (GetComponent<VKButton>());
  104. }
  105. }
这里有个 动画效果 我是自己使用的hotween整合的一些东西,如果你想用自己的可以自己去做。hotween插件 我就不用公布了,可以去官网进行下载,各大论坛也有下载资源。
  1. [cpp] 
  2. using UnityEngine;
  3. using System.Collections;
  4. using Holoville.HOTween;
  5. public enum VkAniDuration{
  6. AnimationCurve = EaseType.AnimationCurve,
  7. EaseInBack=EaseType.EaseInBack,
  8. EaseInBounce=EaseType.EaseInBounce,
  9. EaseInCirc=EaseType.EaseInCirc,
  10. EaseInCubic=EaseType.EaseInCubic,
  11. EaseInElastic=EaseType.EaseInElastic,
  12. EaseInExpo=EaseType.EaseInExpo,
  13. EaseInOutBack=EaseType.EaseInOutBack,
  14. EaseInOutBounce=EaseType.EaseInOutBounce,
  15. EaseInOutCirc=EaseType.EaseInOutCirc,
  16. EaseInOutCubic=EaseType.EaseInOutCubic,
  17. EaseInOutElastic=EaseType.EaseInOutElastic,
  18. EaseInOutExpo=EaseType.EaseInOutExpo,
  19. EaseInOutQuad=EaseType.EaseInOutQuad,
  20. EaseInOutQuart=EaseType.EaseInOutQuart,
  21. EaseInOutQuint=EaseType.EaseInOutQuint,
  22. EaseInOutSine=EaseType.EaseInOutSine,
  23. EaseInQuad=EaseType.EaseInQuad,
  24. EaseInQuart=EaseType.EaseInQuart,
  25. EaseInQuint=EaseType.EaseInQuint,
  26. EaseInSine=EaseType.EaseInSine,
  27. EaseOutBack=EaseType.EaseOutBack,
  28. EaseOutBounce=EaseType.EaseOutBounce,
  29. EaseOutCirc=EaseType.EaseOutCirc,
  30. EaseOutCubic=EaseType.EaseOutCubic,
  31. EaseOutElastic=EaseType.EaseOutElastic,
  32. EaseOutExpo=EaseType.EaseOutExpo,
  33. EaseOutQuad=EaseType.EaseOutQuad,
  34. EaseOutQuart=EaseType.EaseOutQuart,
  35. EaseOutQuint=EaseType.EaseOutQuint,
  36. EaseOutSine=EaseType.EaseOutSine,
  37. Linear=EaseType.Linear
  38. };
  39. public class VKAnimator {
  40. //  public static Tweener moveTo(GameObject obj,float time,Vector3 target,bool isLocal,VkAniDuration vkDurType = VkAniDuration.AnimationCurve){
  41. //      Tweener tw = null;
  42. //      if(isLocal)
  43. //          tw = startVkAnimote(obj,time,"localPosition",target,vkDurType,null);
  44. //
  45. //      else
  46. //          tw = startVkAnimote(obj,time,"position",target,vkDurType,null);
  47. //      return tw;
  48. //  }
  49. public static Tweener moveTo(GameObject obj,float time,Vector3 target,bool isLocal,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  50. Tweener tw = null;
  51. if(isLocal)
  52. tw =startVkAnimote(obj,time,"localPosition",target,vkDurType,completeObj);
  53. else
  54. tw = startVkAnimote(obj,time,"position",target,vkDurType,completeObj);
  55. return tw;
  56. }
  57. public static Tweener moveBy(GameObject obj,float time,Vector3 offset,bool isLocal,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  58. Tweener tw = null;
  59. if(isLocal){
  60. Vector3 tLocalPos = obj.transform.localPosition + offset;
  61. tw = startVkAnimote(obj,time,"localPosition",tLocalPos,vkDurType,completeObj);
  62. }else{
  63. Vector3 tPos = obj.transform.position + offset;
  64. tw = startVkAnimote(obj,time,"position",tPos,vkDurType,completeObj);
  65. }
  66. return tw;
  67. }
  68. public static Tweener scaleTo(GameObject obj,float time,Vector3 scale,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  69. Tweener tw = null;
  70. tw = startVkAnimote(obj,time,"localScale",scale,vkDurType,completeObj);
  71. return tw;
  72. }
  73. public static Tweener scaleBy(GameObject obj,float time,Vector3 offsetScale,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  74. Tweener tw = null;
  75. Vector3 targetScale = obj.transform.localScale+offsetScale;
  76. tw = startVkAnimote(obj,time,"localScale",targetScale,vkDurType,completeObj);
  77. return tw;
  78. }
  79. public static Tweener routeTo(GameObject obj,float time,Vector3 routeEulerTarget,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  80. Tweener tw = null;
  81. tw = startVkAnimote(obj,time,"localEulerAngles",routeEulerTarget,vkDurType,completeObj);
  82. return tw;
  83. }
  84. public static Tweener routeBy(GameObject obj,float time,Vector3 routeEulerOffset,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  85. Tweener tw = null;
  86. Vector3 target = obj.transform.localEulerAngles+ routeEulerOffset;
  87. tw = startVkAnimote(obj,time,"localEulerAngles",target,vkDurType,completeObj);
  88. return tw;
  89. }
  90. //localEulerAngles
  91. private static Tweener startVkAnimote(GameObject obj,float time,string key,object target,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){
  92. Tweener tw = null;
  93. TweenParms twp = new TweenParms();
  94. twp.Prop(key,target);
  95. twp.Ease((EaseType)vkDurType);
  96. if(completeObj!=null){
  97. twp.OnComplete(completeObj.sendTargetObj,completeObj.methodName,completeObj.sendobj,SendMessageOptions.DontRequireReceiver);
  98. }
  99. tw =  HOTween.To(obj.transform,time,twp);
  100. return tw;
  101. }
  102. }
  103. [cpp] 
  104. using UnityEngine;
  105. using System.Collections;
  106. public class VKAniComplete{
  107. public GameObject sendTargetObj = null;
  108. public string methodName = null;
  109. public object sendobj = null;
  110. public VKAniComplete(GameObject sendTargetObj,string methodName,object sendobj){
  111. this.sendTargetObj = sendTargetObj;
  112. this.methodName = methodName;
  113. this.sendobj = sendobj;
  114. }
  115. }
下面就是button的编辑器方面了。ok,这里可能有点麻烦。不过还是可以解决的。
首先我们要获取脚本里面的方法怎么获取那。根据c#的MSDN的描述我们可以看到。就是以下方法,这就是 为什么 NGUI 只能找到public属性的方法。
  1. [cpp] 
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Reflection;
  5. public class GetPublic : MonoBehaviour {
  6. static public  MethodInfo[] GetObjectMethods(string selectedObjClass){
  7. if(selectedObjClass==null)return null;
  8. MethodInfo[] methodInfos=null;
  9. if(System.Type.GetType(selectedObjClass)==null){
  10. return methodInfos;
  11. }
  12. methodInfos =  System.Type.GetType(selectedObjClass).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  13. return methodInfos;
  14. }
  15. }
这里就是button编辑器的一些属性的传递,和界面的修改哈。
[cpp]
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.Reflection;
  5. [CustomEditor(typeof(VKButton))]
  6. public class VKButtonEditor : Editor {
  7. public override void OnInspectorGUI ()
  8. {
  9. int pressEventNum= 0;
  10. int selectScriptNum = 0;
  11. int clickEventNum = 0;
  12. int dragStartEveNum = 0;
  13. int dragEveNum = 0;
  14. int dragEndEveNum = 0;
  15. VKButton button = (VKButton)target;
  16. button.buttonTex = EditorGUILayout.ObjectField ("Texture",button.buttonTex,typeof(Texture),true)as Texture;
  17. button.pressButtonTex = EditorGUILayout.ObjectField ("PressButtonTex",button.pressButtonTex,typeof(Texture),true)as Texture;
  18. button.eventObj = EditorGUILayout.ObjectField("eventObj",button.eventObj,typeof(GameObject),true) as GameObject;
  19. button.info = EditorGUILayout.TextField("info",button.info);
  20. button.ancPointx = EditorGUILayout.Slider("AnchorX",button.ancPointx,0.0f,1.0f);
  21. button.ancPointy = EditorGUILayout.Slider("AnchorY",button.ancPointy,0.0f,1.0f);
  22. if(button.buttonTex == null){
  23. button.width=EditorGUILayout.IntField("Width",button.width);
  24. button.height=EditorGUILayout.IntField("Height",button.height);
  25. }
  26. GUILayout.BeginHorizontal();
  27. if(GUILayout.Button("2X")){
  28. button.setScale = 0.5f;
  29. }
  30. if(GUILayout.Button("1X")){
  31. button.setScale = 1f;
  32. }
  33. if(GUILayout.Button("1.5X")){
  34. button.setScale = 0.75f;
  35. }
  36. GUILayout.EndHorizontal();
  37. GUILayout.BeginHorizontal();
  38. if(GUILayout.Button("ChangeName")){
  39. if(button.buttonTex!=null){
  40. button.name = button.buttonTex.name;
  41. }
  42. }
  43. if(GUILayout.Button("Switch ImageView")){
  44. button.switchImageView();
  45. }
  46. GUILayout.EndHorizontal();
  47. if(button.eventObj!=null){
  48. MonoBehaviour[] monoScriptArry =new MonoBehaviour[button.eventObj.GetComponents<MonoBehaviour>().Length+1];// button.eventObj.GetComponents<MonoBehaviour>();
  49. for(int i=0;i<monoScriptArry.Length;i++){
  50. if(i<monoScriptArry.Length-1)
  51. monoScriptArry [i] = button.eventObj.GetComponents<MonoBehaviour>()[i];
  52. if(i == monoScriptArry.Length-1){
  53. monoScriptArry [i] = null;
  54. }
  55. }
  56. if(monoScriptArry !=null&& monoScriptArry.Length>0){
  57. string [] scriptName = new string[monoScriptArry.Length];
  58. for(int i=0;i<scriptName.Length;i++){
  59. if(monoScriptArry[i]!=null){
  60. scriptName[i] = monoScriptArry[i].GetType().ToString();
  61. if(scriptName[i].Equals("VKCamera")){
  62. scriptName[i] = "null";
  63. }
  64. }else{
  65. scriptName[i] = "null";
  66. }
  67. if(scriptName[i].Equals(button.eventScript)){
  68. selectScriptNum = i;
  69. }
  70. }
  71. selectScriptNum = EditorGUILayout.Popup("EventScript",selectScriptNum,scriptName);
  72. button.eventScript = scriptName[selectScriptNum];
  73. MethodInfo[] publicMethodArry;
  74. if(button.eventScript == null || button.eventScript.Equals("null")){
  75. publicMethodArry = new MethodInfo[1];
  76. }else{
  77. publicMethodArry = new MethodInfo[GetPublic.GetObjectMethods(button.eventScript).Length+1];
  78. }
  79. for(int i = 0;i<publicMethodArry.Length;i++){
  80. if(i<publicMethodArry.Length-1){
  81. publicMethodArry[i] =GetPublic.GetObjectMethods(button.eventScript)[i];
  82. }else{
  83. publicMethodArry[i] = null;
  84. }
  85. }
  86. if(publicMethodArry!=null && publicMethodArry.Length>0){
  87. string[] methodArry = new string[publicMethodArry.Length];
  88. for(int i=0;i<methodArry.Length;i++){
  89. if(publicMethodArry[i]!=null){
  90. methodArry[i] = publicMethodArry[i].Name;
  91. }else{
  92. methodArry[i] ="null";
  93. }
  94. if(button.pressEventName !=null && button.pressEventName.Equals(methodArry[i])){
  95. pressEventNum = i;
  96. }
  97. if(button.clickEventName != null && button.clickEventName.Equals(methodArry[i])){
  98. clickEventNum = i;
  99. }
  100. if(button.isDrag){
  101. if(button.dragStartEventName != null && button.dragStartEventName.Equals(methodArry[i])){
  102. dragStartEveNum = i;
  103. }
  104. if(button.dragEventName != null && button.dragEventName.Equals(methodArry[i])){
  105. dragEveNum = i;
  106. }
  107. if(button.dragEndEventName != null && button.dragEndEventName.Equals(methodArry[i])){
  108. dragEndEveNum = i;
  109. }
  110. }
  111. }
  112. pressEventNum = EditorGUILayout.Popup("PressEvent",pressEventNum,methodArry);
  113. clickEventNum = EditorGUILayout.Popup("ClickEvent",clickEventNum,methodArry);
  114. button.pressEventName = methodArry[pressEventNum];
  115. button.clickEventName = methodArry[clickEventNum];
  116. button.isAni = EditorGUILayout.Toggle ("ISAimator",button.isAni);
  117. if(button.isAni){
  118. button.currentAnimator = EditorGUILayout.Popup("ButtonAnimator",button.currentAnimator,button.animatorType);
  119. }
  120. button.isDrag =  EditorGUILayout.Toggle ("ISDrag",button.isDrag);
  121. if(button.isDrag){
  122. //                      EditorGUILayout.LabelField("---------------------------------------------");
  123. dragStartEveNum = EditorGUILayout.Popup("DragStartEvent",dragStartEveNum,methodArry);
  124. dragEveNum = EditorGUILayout.Popup("DragEvent",dragEveNum,methodArry);
  125. dragEndEveNum = EditorGUILayout.Popup("DragEndEvent",dragEndEveNum,methodArry);
  126. button.dragStartEventName = methodArry[dragStartEveNum];
  127. button.dragEventName = methodArry[dragEveNum];
  128. button.dragEndEventName  =methodArry[dragEndEveNum];
  129. }
  130. }else{
  131. pressEventNum = EditorGUILayout.Popup("PressEvent",pressEventNum,new string[]{"null"});
  132. clickEventNum = EditorGUILayout.Popup("ClickEvent",clickEventNum,new string[]{"null"});
  133. }
  134. }
  135. }else{
  136. button.isAni =false;
  137. button.isDrag =false;
  138. }
  139. button.updateButton ();
  140. if(button!=null){
  141. EditorUtility.SetDirty(button);
  142. }
  143. EditorUtility.UnloadUnusedAssets();
  144. }
  145. }
接下来来讲述下怎么去实现一个label.
这个就相对比较简单了,其实我们可以直接使用unity自带得textmesh来实现一个字体,但大多数时候,需要改一些设置,字体大小,材质等等 一些得问题所以我们,最好还是自己写一个脚本来实现一些简单的操作,方便简洁嘛。其实很简单 ,下面我们就开始来实现这些方法。
代码
  1. [cpp] 
  2. using UnityEngine;
  3. using System.Collections;
  4. [RequireComponent(typeof(MeshRenderer))]
  5. [ExecuteInEditMode]
  6. public class VKLabel : MonoBehaviour {
  7. public Font labelFont ;
  8. TextMesh labelMesh;
  9. Material labelMat;
  10. // Use this for initialization
  11. void Start () {
  12. //labelFont = new Font ("Arial");
  13. labelMesh = GetComponent<TextMesh>();
  14. if(labelMesh == null){
  15. labelMesh = gameObject.AddComponent<TextMesh>();
  16. }
  17. labelFont = labelMesh.font;
  18. if(labelMesh.text.Length==0){
  19. labelMesh.text ="VKLabel";
  20. }
  21. labelMat = new Material(Shader.Find("VK/VkLabelShader"));
  22. labelMat.mainTexture = labelMesh.font.material.mainTexture;
  23. GetComponent<MeshRenderer>().material = labelMat;
  24. updateTextMesh();
  25. }
  26. public void updateTextMesh(){
  27. if(labelMesh!=null){
  28. labelMesh.font = labelFont;
  29. }
  30. if(labelMat!=null && labelFont!=null){
  31. if(labelFont.material!=null){
  32. labelMat.mainTexture = labelFont.material.mainTexture;
  33. }
  34. labelMat.color = labelMesh.color;
  35. GetComponent<MeshRenderer>().material = labelMat;
  36. }
  37. }
  38. }
VkLabelShader是自己写得一个shader,你也可以用unity自己带得 GUI得字体shader。为了好控制一些,决定还是用自己得shader了。
  1. [cpp]
  2. Shader "VK/VkLabelShader" {
  3. Properties {
  4. _MainTex ("Font Texture", 2D) = "white" {}
  5. _Color ("Text Color", Color) = (1,1,1,1)
  6. }
  7. SubShader {
  8. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  9. Lighting Off Cull Off ZWrite Off Fog { Mode Off }
  10. Blend SrcAlpha OneMinusSrcAlpha
  11. Pass {
  12. Color [_Color]
  13. SetTexture [_MainTex] {
  14. combine primary, texture * primary
  15. }
  16. }
  17. }
  18. }
下面我们要做得就是 在写一个简单得label得编辑器类。使label得监视面板更好看点。
这里就不写了 因为我的这个 是没有加任何得东西进行修饰得。
注意下 这里 不要使用 [RequireComponent(typeof(TextMesh))] 添加组件不然,你的TextMesh是没有任何字体得。
要使用unity自带得方法来进行添加组件,这样它会自动帮你,加上unity内置得字体。