NGUI的UISprite動態染色的一種方法


本文主要參考iwinterice 的 NGUI的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);
        }

效果圖:     是死灰死灰的吧。。。。。。。

后來,被告知,還要去改透明度,就是呃,變灰之后,就需要半透明效果。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM