Unity2D:简单人物纸娃娃换装实现(一) 服装的变换
效果預(yù)覽
基本效果?
鄙人不才,實(shí)現(xiàn)的方法較為粗暴,如果有更好的方案還望大神指教一二。
?
準(zhǔn)備工作
巧婦難為無(wú)米之炊,制作換裝系統(tǒng)首先得有“裝備”才可以。如果大家會(huì)美術(shù)可以自己畫(huà),注意所有的素材的遮擋順序和運(yùn)動(dòng)細(xì)節(jié)需要一致,當(dāng)然如果用程序控制遮罩來(lái)實(shí)現(xiàn)更為優(yōu)秀的遮擋管理我覺(jué)得也是可行的。
給大家看看我用的素材集合(自制)截圖:
有了這些替換用的素材就可以開(kāi)始實(shí)現(xiàn)換裝了。
?
Plyer的建立
我將人物拆成了各個(gè)不同的身體部件,它們每一個(gè)都是一個(gè)SpriteRander,手部因?yàn)楸容^復(fù)雜所以拆的尤其的多。(這里的DrawCall可能會(huì)很高,用Shader來(lái)合并素材應(yīng)該能節(jié)省性能,可惜我不會(huì)(*^_^*))
建立好的人物建立好Player之后,請(qǐng)手動(dòng)確保渲染順序(OrderInLayer)的正確性。
接下來(lái)要給人物建立好不同的動(dòng)畫(huà),譬如走路跳躍攻擊。這一步關(guān)于Unity本身動(dòng)畫(huà)狀態(tài)機(jī)的使用相信大家都會(huì)的。
?
素材庫(kù)的建立
使用類(lèi)似的腳本來(lái)存儲(chǔ)圖片:
using System.Collections; using System.Collections.Generic; using UnityEngine;//用以存儲(chǔ)身體圖片的類(lèi) public class Doll_body : MonoBehaviour {//圖片的存儲(chǔ)結(jié)構(gòu)體//使用此注解使其能顯示在Unity的編輯面板上[System.Serializable]public struct DollBody{public Sprite bodyNormal;public Sprite bodyFront;public Sprite leftHandNormal;public Sprite leftHandBack;public Sprite leftHandFront;public Sprite leftHand_SwordAttackStab_prepare;public Sprite leftHand_SwordAttackStab_attack;public Sprite leftHand_SwordAttackCleave_up;public Sprite leftHand_SwordAttackCleave_mid;public Sprite leftHand_bowAttack_01;public Sprite leftHand_bowAttack_02;public Sprite leftHand_bowAttack_03;public Sprite rightHandFront;public Sprite rightHandNormal;public Sprite rightHand_holdBow;}//不同的裝備public DollBody piBody;public DollBody xunlinBody;public DollBody zibiBody; }創(chuàng)建一個(gè)空物體名為“換裝管理器?”,給它加上上述組件,在拖動(dòng)圖片賦值,切換場(chǎng)景時(shí)注意不要銷(xiāo)毀此物體。(因?yàn)槭窍袼仫L(fēng)游戲,所以圖集比較小,大游戲的話(huà)也許得用更高效的素材庫(kù)了,具體怎么實(shí)現(xiàn)沒(méi)有思路)
使用腳本時(shí)的界面?
創(chuàng)建下面這個(gè)腳本,同樣作為“換裝管理器?”的組件,用來(lái)給各個(gè)圖片組起個(gè)名字,方便調(diào)用。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class DollDictionary : MonoBehaviour {public static Dictionary<string,Doll_foot.DollFoot> footDictionary = new Dictionary<string, Doll_foot.DollFoot>();public static Dictionary<string,Doll_body.DollBody> bodyDictionary = new Dictionary<string, Doll_body.DollBody> ();public static Dictionary<string,Doll_hair.DollHair> hairDictionary = new Dictionary<string, Doll_hair.DollHair>();public static Dictionary<string,Doll_helmet.DollHelmet> helmetDictionary =new Dictionary<string, Doll_helmet.DollHelmet>();public static Dictionary<string,Doll_Skin.DollSkin> skinDictionary = new Dictionary<string, Doll_Skin.DollSkin>();void Awake () {Doll_foot tempDollFoot = this.GetComponent<Doll_foot> ();footDictionary.Add ("皮質(zhì)腿甲", tempDollFoot.piFoot);footDictionary.Add ("巡林腿甲", tempDollFoot.xunlinFoot);footDictionary.Add("緊身褲", tempDollFoot.jinshengFoot);Doll_body tempDollBody = this.GetComponent<Doll_body> () ;bodyDictionary.Add ("皮質(zhì)胸甲", tempDollBody.piBody);bodyDictionary.Add ("巡林胸甲", tempDollBody.xunlinBody);bodyDictionary.Add("自閉胸甲", tempDollBody.zibiBody);Doll_hair tempDollHair = this.GetComponent<Doll_hair> ();hairDictionary.Add ("普通", tempDollHair.putongHair);hairDictionary.Add ("半遮", tempDollHair.banzheHair);hairDictionary.Add ("丸子", tempDollHair.wanziHair);hairDictionary.Add ("莫辛甘", tempDollHair.moxingganHair);hairDictionary.Add("飛機(jī)頭", tempDollHair.feijiHair);Doll_helmet tempDollHelmet = this.GetComponent<Doll_helmet> ();helmetDictionary.Add ("兜帽", tempDollHelmet.doumaoHelmet);helmetDictionary.Add ("巫師帽", tempDollHelmet.fashiHelmet);helmetDictionary.Add ("鐵頭盔", tempDollHelmet.metalHelmet);helmetDictionary.Add ("皮帽", tempDollHelmet.piHelmett);Doll_Skin tempDollSkin = this.GetComponent<Doll_Skin> ();skinDictionary.Add ("皮膚A", tempDollSkin.SkinTyprA);skinDictionary.Add ("皮膚B", tempDollSkin.SkinTyprB);skinDictionary.Add ("皮膚C", tempDollSkin.SkinTyprC);} }?
添加動(dòng)畫(huà)事件
給Player附上下面這個(gè)腳本,用以在特定的時(shí)候改變?nèi)宋锏膱D片。
其中SetBody(PlayerAttribute),用以改變?nèi)宋锏膱D集,PlayerAttribute是我自己定義的人物狀態(tài)類(lèi),當(dāng)里面的字段改變時(shí)會(huì)自動(dòng)出發(fā)一個(gè)事件來(lái)調(diào)用SetBody更改圖集。
setBodyXXX(),用以在動(dòng)畫(huà)中觸發(fā)改變圖片,眾所周知,動(dòng)畫(huà)就是一系列變化的圖片,這樣便實(shí)現(xiàn)了動(dòng)態(tài)。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class SwarpSprites : MonoBehaviour {[SerializeField]SpriteRenderer helmetRender,hairRender,skinHeadRender,bodyRender,footRender;Sprite walkMid,walkFront,walkBack,footStand,footFall;Sprite bodyNormal,bodyFront;Sprite skinHeadNormal,skinHeadUp,skinHeadDown;Sprite helmetNormal,helmetUp,helmetDown;Sprite hairNormal,hairUp,hairDown;public SwarpHandSprites swarpHandSprites;void Start () {setAll ();}public void setAll(){PlayerAttribute playerAttribute = this.GetComponent<PlayerAttribute> ();setFoot (playerAttribute);setBody (playerAttribute);setSkin (playerAttribute);setHelmet (playerAttribute);setHair (playerAttribute);}public void setBody(PlayerAttribute playerAttribute){Doll_body.DollBody bodySet = DollDictionary.bodyDictionary [playerAttribute.bodyType];bodyNormal = bodySet.bodyNormal;bodyFront = bodySet.bodyFront;swarpHandSprites.bodyLeftHandNormal = bodySet.leftHandNormal;swarpHandSprites.bodyLeftHandFront = bodySet.leftHandFront;swarpHandSprites.bodyLeftHandBack = bodySet.leftHandBack;swarpHandSprites.bodyLeftHand_SwordAttackStab_prepare = bodySet.leftHand_SwordAttackStab_prepare;swarpHandSprites.bodyLeftHand_SwordAttack_attack = bodySet.leftHand_SwordAttackStab_attack;swarpHandSprites.bodyLeftHand_SwardAttackCleave_mid = bodySet.leftHand_SwordAttackCleave_mid;swarpHandSprites.bodyLeftHand_SwardAttackCleave_Up = bodySet.leftHand_SwordAttackCleave_up;swarpHandSprites.bodyLeftHand_bowAttack01 = bodySet.leftHand_bowAttack_01;swarpHandSprites.bodyLeftHand_bowAttack02 = bodySet.leftHand_bowAttack_02;swarpHandSprites.bodyLeftHand_bowAttack03 = bodySet.leftHand_bowAttack_03;swarpHandSprites.bodyRightHandNormal = bodySet.rightHandNormal;swarpHandSprites.bodyRightHandFront = bodySet.rightHandFront;swarpHandSprites.bodyRightHand_holdBow = bodySet.rightHand_holdBow;}//設(shè)置足部動(dòng)畫(huà)void setWalkMid(){ footRender.sprite = walkMid; }void setWalkFront(){ footRender.sprite = walkFront; }void setWalkBack(){ footRender.sprite = walkBack; }void setFootStand(){ footRender.sprite = footStand; }void setFootFallDown(){ footRender.sprite = footFall; }//設(shè)置身體動(dòng)畫(huà)public void setBodyNormal(){ bodyRender.sprite = bodyNormal; }void setBodyFront(){ bodyRender.sprite = bodyFront; }//還有很多......}?
在合適的關(guān)鍵幀調(diào)用切換圖片的函數(shù)?
以上,就實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的紙娃娃,最耗時(shí)的,其實(shí)是繪畫(huà)各種替換素材的過(guò)程。
總結(jié)
以上是生活随笔為你收集整理的Unity2D:简单人物纸娃娃换装实现(一) 服装的变换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实现一个无法被继承的C++类
- 下一篇: 为什么苹果不再需要谷歌地图?