您的位置:首页 > 产品设计 > UI/UE

NGUI的UISprite动态染色的一种方法

2016-05-11 17:43 411 查看

http://www.cnblogs.com/slysky/p/4568648.html

本文主要参考iwintericeNGUI的UISprite动态染色的一种解决方案 文章。
参考参考,就是既参详又拷贝,-,-|||
相关理论依据,还请去移步 NGUI的UISprite动态染色的一种解决方案 里面去寻找。我这里只有提供一下源码,并对原博文进行了一点点修改,至于修改后的好坏,俺也不知道咯。
首先阐明一下需求,就是几个角色,打怪,扫怪,头像一直blingbling的闪,但是,duang,血量爆掉了,角色挂了,头像就得死灰死灰的样子了。
原博文是去改了UISprite的源码,虽然本人很想去改,但本着不能随意修改源码的原则(因为,你不知道你修改之后对其他人会造成什么影响,而且也为了保持源码的简洁),于是,继承咯

1 //----------------------------------------------
2 //            NGUI: Next-Gen UI kit
3 // Copyright © 2011-2015 Tasharen Entertainment
4 //----------------------------------------------
5
6 using UnityEngine;
7 using System.Collections.Generic;
8
9 /// <summary>
10 /// Sprite is a textured element in the UI hierarchy.
11 /// 可以赋值一个ColorMaterial
12 /// </summary>
13
14 [ExecuteInEditMode]
15 [AddComponentMenu("NGUI/UI/NGUI SpriteExt")]
16 public class UISpriteExt : UISprite
17 {
18     /// <summary>
19     /// Retrieve the material used by the font.
20     /// </summary>
21
22     [HideInInspector][SerializeField]private Material mColorMaterial;
23     public Material ColorMaterial
24     {
25         get { return mColorMaterial; }
26         set { mColorMaterial = value; }
27     }
28     [System.NonSerialized]bool mIsShowColor = false;
29     public bool IsShowColor
30     {
31         get { return mIsShowColor; }
32         set { mIsShowColor = value; }
33     }
34     public override Material material
35     {
36         //get { return (mAtlas != null) ? mAtlas.spriteMaterial : null; }
37         get
38         {
39             Material mat = base.material;
40             if ( mat == null )
41             {
42                 mat = (mAtlas != null) ? mAtlas.spriteMaterial : null;
43             }
44
45             if (mColorMaterial != null && mIsShowColor)
46             {
47                 ColorMaterial.SetTexture(0, mat.GetTexture(0));
48                 return ColorMaterial;
49             }
50             else
51             {
52                 return mat;
53             }
54         }
55
56     }
57
58     GameObject GetMostClosePanel(Transform rootTrans)
59     {
60         if (rootTrans.GetComponent<UIPanel>() != null)
61         {
62             return rootTrans.gameObject;
63         }
64         else if (rootTrans.parent == null)
65         {
66             return null;
67         }
68         else
69         {
70             return GetMostClosePanel(rootTrans.parent);
71         }
72     }
73
74     GameObject panelObj = null;
75     public bool selfRefresh = true;
76
77     public void RefreshPanel(GameObject go)
78     {
79         if (!selfRefresh)
80             return;
81
82         if (panelObj == null)
83         {
84             panelObj = GetMostClosePanel(go.transform);
85         }
86
87         if (panelObj != null)
88         {
89             panelObj.GetComponent<UIPanel>().enabled = false;
90             panelObj.GetComponent<UIPanel>().enabled = true;
91             go.SetActive(false);
92             go.SetActive(true);
93         }
94     }
95 }


自古以来,起名字就是个文雅的活,本博只是一个有文化的流氓,所以起的名字也比较流氓(=@__@=)哪

有了基本类,是远远不够的,还需要一个Editor内的类,o(╯□╰)o,比较简单,快来瞅瞅~~

1 using UnityEngine;
2 using UnityEditor;
3 using System.Collections.Generic;
4
5 /// <summary>
6 /// Inspector class used to edit UISpriteExts.
7 /// </summary>
8
9 [CanEditMultipleObjects]
10 [CustomEditor(typeof(UISpriteExt), true)]
11 public class UISpriteExtInspector : UISpriteInspector
12 {
13     /// <summary>
14     /// Draw the atlas and sprite selection fields.
15     /// </summary>
16
17     protected override bool ShouldDrawProperties ()
18     {
19         base.ShouldDrawProperties();
20
21         SerializedProperty wm = NGUIEditorTools.DrawProperty("WMaterial", serializedObject, "mColorMaterial", GUILayout.MinWidth(20f));
22         if ( wm!= null )
23         {
24             Material mColorMaterial = wm.objectReferenceValue as Material;
25         }
26         GUILayout.Space(6f);
27
28         return true;
29     }
30 }


用的时候,就是直接将材质球拖到WMaterial内咯,



看,连材质球,都是借用的原博文内的东西。我实在是个懒人。
我还提供了 mIsShowColor 这个变量,用来控制显示。使用的示例代码如下:

// 灰度图像
public void ShowAsGray()
{
spriteIcon.IsShowColor = true;
spriteIcon.RefreshPanel(spriteIcon.gameObject);
}
// 恢复正常图像
public void ShowAsNormal()
{
spriteIcon.IsShowColor = false;
spriteIcon.RefreshPanel(spriteIcon.gameObject);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: