二次元日系游戏制作工具 - live2dSDK项目实战
導入素材創建場景
游戲開始場景制作
星星閃耀動畫的制作
特效孵化器
添加一個EffectSpawn腳本,用來潑撒花瓣和愛心
using System.Collections; using System.Collections.Generic; using UnityEngine;/// <summary> /// 特效孵化器 /// </summary> public class EffectSpawn : MonoBehaviour {public GameObject[] effectGos;public Transform canvasTrans;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}private void CreateEffectGo(){int randomIndex = Random.Range(0, 2);GameObject effectGo = Instantiate(effectGos[randomIndex], transform.position, transform.rotation);effectGo.transform.SetParent(canvasTrans);} }在屏幕右上角創建空物體,將腳本掛載在它上面,并添加預制體
特效的移動腳本
在EffectSpawn腳本的Start力添加以下代碼,讓花瓣和愛心兩秒隨機生成一次
InvokeRepeating("CreateEffectGo", 0, 2);在花瓣和愛心預制體上創建新的腳本EffectMove,用來控制物體的移動
using System.Collections; using System.Collections.Generic; using UnityEngine;/// <summary> /// 特效移動 /// </summary> public class EffectMove : MonoBehaviour {public float moveSpeed = 5;// Use this for initializationvoid Start () {Destroy(gameObject, 10);}// Update is called once per framevoid Update () {transform.Translate(-transform.right * moveSpeed * Time.deltaTime);} }特效的完善
首先在EffectSpawn里修改孵化器的角度為45度
transform.rotation = Quaternion.Euler(new Vector3(0, 0, Random.Range(0, 45)));然后在EffectMove里添加復合運動的代碼,讓特效看起來更真實
private float timeVal;private int randomYPos;void Update () {transform.Translate(-transform.right * moveSpeed * Time.deltaTime);if(timeVal >= 1){timeVal = 0;randomYPos = Random.Range(-1, 2);}else{transform.Translate(transform.up * randomYPos * Time.deltaTime * moveSpeed / 5);timeVal += Time.deltaTime;}}添加音樂,然后給Buttont添加加載Game場景的代碼
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;/// <summary> /// 開始按鈕的功能 /// </summary> public class LoadGame : MonoBehaviour {public void OnButtonStartClick(){SceneManager.LoadScene(1);}}點擊Start就加載到Game場景
搭建游戲場景導入Live2d框架
使用框架來加載Live2d模型
輸入模型Json文件的地址就可以顯示出來模型
完成金幣顯示的UI
游戲場景UI的制作
游戲管理的創建
創建GameManager腳本用來管理金幣、好感度和日期。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;public class GameManager : MonoBehaviour {//單例private static GameManager _instance;public static GameManager Instance{get{return _instance;}}//玩家屬性public int gold;public int favor;public int leftDays;public Text goldText;public Text favorTest;public Text dateText;private void Awake(){_instance = this;gold = favor = 0;leftDays = 20;}// Update is called once per framevoid Update(){} }更新文本UI的方法
在GamaManger里面添加更新UI的方法
//更新玩家屬性UI顯示private void UpdateUI(){goldText.text = gold.ToString();favorTest.text = favor.ToString();dateText.text = leftDays.ToString();}//金幣數額的變化方法public void ChangeGold(int goldValue){gold += goldValue;if (gold <= 0)gold = 0;UpdateUI();}//好感度數額的變化方法public void ChangeFavor(int favorValue){favor += favorValue;if (favor <= 0)favor = 0;UpdateUI();}天亮天黑方法
首先添加三個狀態來控制天亮天黑
//天黑天亮屬性public Image mask;private bool toAnotherDay;private bool toBeDay;添加天亮天黑的方法
//天黑public void ToDark(){mask.color += new Color(0, 0, 0, Mathf.Lerp(0, 1, 0.1f));if(mask.color.a >= 0.8f){mask.color = new Color(0, 0, 0, 1);toBeDay = true;UpdateUI();}}//天亮public void ToDay(){mask.color -= new Color(0, 0, 0, Mathf.Lerp(1, 0, 0.1f));if (mask.color.a <= 0.2f){mask.color = new Color(0, 0, 0, 0);toAnotherDay = false;toBeDay = false;}}天黑天亮方法的具體運行與打工按鈕的制作
完善天亮天黑的辦法
//天黑天亮屬性public Image mask;public bool toAnotherDay;public bool toBeDay;private float timeVal; void Update(){//是否過渡到另外一天if (toAnotherDay){if (toBeDay){//天亮if (timeVal >= 2){ToDay();timeVal = 0;}else{timeVal += Time.deltaTime;}}else{//天黑ToDark();}}}添加打工的分類
打工與對話框UI的制作
打工按鈕事件的處理
public GameObject actionBtns;//工作public GameObject workBtns;添加方法ClickWorkBtn()
public void ClickWorkBtn(){actionBtns.SetActive(false);workBtns.SetActive(true);}打工后獲取金幣的方法
首先添加幾個屬性
public LAppModelProxy lAppModelProxy;public GameObject talkLine;public Text talkLineText;//工作public GameObject workBtns;public Sprite[] workSprites;public Image workImage;public GameObject workUI;添加點擊工作以及賺錢的代碼
public void ClickWorkBtn(){actionBtns.SetActive(false);workBtns.SetActive(true);lAppModelProxy.SetVisible(false);//隱藏模型}public void GetMoney(int workIndex){workBtns.SetActive(false);ChangeGold((4 - workIndex) * 20);workImage.sprite = workSprites[workIndex];workUI.SetActive(true);talkLine.SetActive(true);talkLineText.text = "勞動最光榮";}打工流程的測試
添加一個即將天黑的代碼,用來點擊控制toAnotherDya
//即將天黑public void ToBeDark(){toAnotherDay = true;}給各種屬性進行賦值
進行測試
重置所有UI顯示的方法
添加重置UI顯示的辦法
//重置所有UIprivate void ResetUI(){workUI.SetActive(false);talkLine.SetActive(false);actionBtns.SetActive(true);lAppModelProxy.SetVisible(true);leftDays--;UpdateUI();}并在天黑的代碼里進行調用
//天黑public void ToDark(){mask.color += new Color(0, 0, 0, Mathf.Lerp(0, 1, 0.1f));if(mask.color.a >= 0.8f){mask.color = new Color(0, 0, 0, 1);toBeDay = true;UpdateUI();ResetUI();}}聊天UI的制作
聊天按鈕的事件
添加聊天的屬性
//聊天public GameObject chatUI;添加聊天按鈕的事件
//聊天public void ClickChatBtn(){actionBtns.SetActive(false);chatUI.SetActive(true);}使用框架的方法播放動作
根據好感度不同播放不同的動畫
//聊天public void ClickChatBtn(){actionBtns.SetActive(false);chatUI.SetActive(true);if(favor >= 100){lAppModelProxy.GetModel().StartMotion("tap_body", 1, 2);}else{lAppModelProxy.GetModel().StartMotion("tap_body", 0, 2);}}使用框架的方法設置表情
public void GetFavor(int chatIndex){chatUI.SetActive(false);talkLine.SetActive(true);switch (chatIndex){case 0:if (favor > 20){ChangeFavor(10);talkLineText.text = "謝謝啊,二狗子,你也很帥。。。";}else{ChangeFavor(2);talkLineText.text = "哦,謝謝。";lAppModelProxy.GetModel().SetExpression("f08");}break;case 1:if (favor > 60){ChangeFavor(20);talkLineText.text = "啊。。哦,不好意思,謝謝哈。。。";lAppModelProxy.GetModel().SetExpression("f07");}else{ChangeFavor(2);talkLineText.text = "哦,謝謝。";lAppModelProxy.GetModel().SetExpression("f08");}break;default:break;}}聊天功能的完善
public void GetFavor(int chatIndex){chatUI.SetActive(false);talkLine.SetActive(true);switch (chatIndex){case 0:if (favor > 20){ChangeFavor(10);talkLineText.text = "謝謝啊,二狗子,你也很帥。。。";}else{ChangeFavor(2);talkLineText.text = "哦,謝謝。";lAppModelProxy.GetModel().SetExpression("f08");}break;case 1:if (favor > 60){ChangeFavor(20);talkLineText.text = "啊。。哦,不好意思,謝謝哈。。。";lAppModelProxy.GetModel().SetExpression("f07");}else{ChangeFavor(-20);talkLineText.text = "你看錯了!你手拿開,真不禮貌。";lAppModelProxy.GetModel().SetExpression("f03");}break;case 2:if (favor > 100){ChangeFavor(40);talkLineText.text = "那。。咱們一起去吃飯,下午去哪玩?";lAppModelProxy.GetModel().SetExpression("f05");}else{ChangeFavor(-40);talkLineText.text = "你這人說話怎么這樣啊,我又沒得罪你。";lAppModelProxy.GetModel().SetExpression("f04");}break;default:break;}解決文本因自動適應而字體過小的方法
在代碼里面添加"\n"換行即可。
在ResetUI里添加表情重置。
lAppModelProxy.GetModel().SetExpression("f01");學校門口約會的情況
首先定義個兩個約會使用到的變量
//約會public SpriteRenderer bgImage;public Sprite[] dateSprites;然后開始寫約會的方法
/// <summary>/// 約會/// </summary>public void ClickDataBtn(){actionBtns.SetActive(false);talkLine.SetActive(true);int randomNum = Random.Range(1, 4);bool hasEnoughGold = false;bgImage.sprite = dateSprites[randomNum];switch(randomNum){case 1:if(gold >= 50){ChangeGold(-50);ChangeFavor(150);talkLineText.text = "學校門口原來有這么好玩的地方。" + "\n" +"今天謝謝你了,二狗子。";hasEnoughGold = true;}else{talkLineText.text = "沒事,不用在意,我最近零花錢比較多。";ChangeGold(-50);}break;default:break;}}約會方法的完善
/// <summary>/// 約會/// </summary>public void ClickDataBtn(){actionBtns.SetActive(false);talkLine.SetActive(true);int randomNum = Random.Range(1, 4);bool hasEnoughGold = false;bgImage.sprite = dateSprites[randomNum];switch(randomNum){case 1:if(gold >= 50){ChangeGold(-50);ChangeFavor(150);talkLineText.text = "學校門口原來有這么好玩的地方。" + "\n" +"今天謝謝你了,二狗子。";hasEnoughGold = true;}else{talkLineText.text = "沒事,不用在意,我最近零花錢比較多。";ChangeGold(-50);}break;case 2:if (gold >= 150){ChangeGold(-150);ChangeFavor(300);talkLineText.text = "蟹黃湯包,烤鴨還有其他的甜品真的都太好吃了!" + "\n" +"謝謝招待!";hasEnoughGold = true;}else{talkLineText.text = "下次有機會你再請我吃飯吧。";ChangeFavor(-150);}break;case 3:if (gold >= 300){ChangeGold(-300);ChangeFavor(500);talkLineText.text = "今天真的很開心," + "\n" +"還有,謝謝你送的禮物,你人真好。。。";hasEnoughGold = true;}else{talkLineText.text = "那個娃娃真的好可愛哦,好想要。。。";ChangeFavor(-300);}break;default:break;}if (hasEnoughGold){lAppModelProxy.GetModel().StartMotion("pinch_in", 0, 2);}else{lAppModelProxy.GetModel().StartMotion("flick_head", 0, 2);}}表白方法的編寫
/// <summary>/// 表白/// </summary>public void ClickLoveBtn(){actionBtns.SetActive(false);talkLine.SetActive(true);if(favor >= 1500){//表白成功talkLineText.text = "謝謝你啊,二狗子。其實我也喜歡你很久了," + "/n" +"自己喜歡的那個人也正好喜歡著自己," + "\n" +"真好,希望你可以讓我永遠陪著你。";lAppModelProxy.GetModel().StartMotion("pinch_out", 0, 2);}表白功能的測試
完善表白功能的代碼
/// <summary>/// 表白/// </summary>public void ClickLoveBtn(){actionBtns.SetActive(false);talkLine.SetActive(true);if(favor >= 1500){//表白成功talkLineText.text = "謝謝你啊,二狗子。其實我也喜歡你很久了," + "/n" +"自己喜歡的那個人也正好喜歡著自己," + "\n" +"真好,希望你可以讓我永遠陪著你。";lAppModelProxy.GetModel().StartMotion("pinch_out", 0, 2);lAppModelProxy.GetModel().SetExpression("f07");}else{//表白失敗talkLineText.text = "二狗子,你。。你," + "\n" +"突然的表白的嚇我一跳" + "\n" +"你真的喜歡我是嗎?" + "\n" +"可是。。。我們還不夠了解彼此。。。";lAppModelProxy.GetModel().StartMotion("shake", 0, 2);lAppModelProxy.GetModel().SetExpression("f04");}}野生Live2d模型素材文件的處理與BOSS腳本的創建
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Live2DSimpleModel : MonoBehaviour {public TextAsset modelFile;public Texture2D texture;public TextAsset idleMotionFile;public GameObject manatsu;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){} }Boss腳本的成員變量的定義與Start方法的編寫
using System.Collections; using System.Collections.Generic; using UnityEngine; using live2d;public class Live2DSimpleModel : MonoBehaviour {public TextAsset modelFile;public Texture2D texture;public TextAsset idleMotionFile;public GameObject manatsu;private Live2DModelUnity live2DModel;private Matrix4x4 live2DCanvasPos;private Live2DMotion live2DMotionIdle;private MotionQueueManager motionQueueManager;private EyeBlinkMotion eyeBlinkMotion;// Start is called before the first frame updatevoid Start(){Live2D.init();live2DModel = Live2DModelUnity.loadModel(modelFile.bytes);live2DModel.setTexture(0, texture);float modelWidth = live2DModel.getCanvasWidth();live2DCanvasPos = Matrix4x4.Ortho(0, modelWidth, modelWidth, 0, -50.0f, 50.0f);live2DMotionIdle = Live2DMotion.loadMotion(idleMotionFile.bytes);live2DMotionIdle.setLoop(true);motionQueueManager = new MotionQueueManager();eyeBlinkMotion = new EyeBlinkMotion();motionQueueManager.startMotion(live2DMotionIdle);}// Update is called once per framevoid Update(){} }Boss渲染的完成與位置和比例的設置
完善Boss的代碼
// Update is called once per framevoid Update(){live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);motionQueueManager.updateParam(live2DModel);eyeBlinkMotion.setParam(live2DModel);live2DModel.update();}private void OnRenderObject(){live2DModel.draw();}Boss移動方法的編寫
首先創建幾個變量
public float moveSpeed;private Vector3 initPos;//判斷當前Boss是否被打敗private bool isDefeat; initPos = transform.position;然后在Update里面添加Boss移動的方法
if (GameManager.Instance.gameOver){return;}//判斷當前Boss是否追趕上Manatsuif (manatsu.transform.position.x - transform.position.x < 3){GameManager.Instance.gameOver = true;}if (isDefeat){transform.position = Vector3.Lerp(transform.position, initPos, 0.2f);}else{transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);}擊打Boss的方法
創建一個計數變量,用來記錄打擊次數
private int hitCount; private void OnMouseDown(){if (GameManager.Instance.gameOver){return;}if (hitCount >= 20){isDefeat = true;}else{hitCount++;}}在Manatsu身上的腳本添加下列屬性,以便Manatsu淘寶
public bool isRunningAway;public float moveSpeed;Manatsu的逃跑方法
首先在GameManager中添加一個屬性,用來控制BadBoy
public Live2DSimpleModel badBoyScript;在Manatsu身上的腳本中添加逃跑方法,首要記錄初始位置
initPos = transform.position;然后在Update里添加逃跑方法
if(GameManager.Instance != null){if (GameManager.Instance.gameOver){isRunningAway = false;return;}}if (isRunningAway){transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);}if (GameManager.Instance != null){if (GameManager.Instance.badBoyScript.isDefeat){transform.position = Vector3.Lerp(transform.position, initPos, 0.1f);}}Boss的有關邏輯處理
在GameManager中添加一個對話框
public GameObject badBoyTalkLine;然后添加一個產生壞男孩和關閉對話框的方法
//產生壞男孩private void CreateBadBoy(){lAppModelProxy.isRunningAway = true;badBoyScript.gameObject.SetActive(true);lAppModelProxy.GetModel().SetExpression("f04");actionBtns.SetActive(false);badBoyTalkLine.SetActive(true);}public void CloseBadBoyTalkLine(){badBoyTalkLine.SetActive(false);}然后在ResetUI里面進行調用
//重置所有UIprivate void ResetUI(){workUI.SetActive(false);talkLine.SetActive(false);actionBtns.SetActive(true);lAppModelProxy.SetVisible(true);leftDays--;lAppModelProxy.GetModel().SetExpression("f01");bgImage.sprite = dateSprites[0];UpdateUI();if(leftDays == 5){CreateBadBoy();}}擊敗Boss之后的文本提示與處理
public void DefeatBadBoy(){lAppModelProxy.GetModel().StartMotion("shake", 0, 2);talkLine.SetActive(true);talkLineText.text = "剛才嚇死我了,謝謝你,二狗子" + "\n" +"要不是你及時救我,我就。。。" + "\n" +"你人好勇敢,真帥。。。";ChangeFavor(300);}然后在Boss的腳本里進行調用
if (hitCount >= 20){isDefeat = true;GameManager.Instance.DefeatBadBoy();}鼠標點擊特效的動畫制作
鼠標點擊時產生特效的代碼實現
首先添加兩個變量,一個是預制體,一個是Canvas
//其他public GameObject clickEffect;public Canvas canvas;在Update里添加產生特效的代碼
//產生鼠標點擊特效if (Input.GetMouseButtonDown(0)){Vector2 mousePos = Vector2.one;RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out mousePos);GameObject go = Instantiate(clickEffect);go.transform.SetParent(canvas.transform);go.transform.localPosition = mousePos;}游戲結束UI的制作
public GameObject gameOverBtns;然后在Update里面進行判斷
//游戲結束邏輯if (gameOver){talkLine.SetActive(true);gameOverBtns.SetActive(true);actionBtns.SetActive(false);}游戲結束邏輯的編寫
//游戲結束邏輯if (gameOver){talkLine.SetActive(true);gameOverBtns.SetActive(true);actionBtns.SetActive(false);if (favor >= 1500){talkLineText.text = "二狗子終于追到了乃木坂最腹黑的釣師——Manatsu。" + "\n" +"最后他們幸福的在一起了!";}else if(leftDays!=0&&favor < 1500){talkLineText.text = "Manatsu受到欺負時二狗子沒有保護她," + "\n" +"從此他們決裂了。";}else{talkLineText.text = "二狗子在出國前沒能獲取到Manatsu的芳心," + "\n" +"于是他們沒能在一起。";}}添加按鈕加載場景的代碼
public void LoadScene(int sceneNum){SceneManager.LoadScene(sceneNum);}給女主角換衣服
首先創建一個用來存儲衣服的變量
public Texture2D manatusNewCloth;然后在ResetUI里面進行使用
else if (leftDays == 10){Live2DModelUnity live2DModelUnity = lAppModelProxy.GetModel().GetLive2DModelUnity();live2DModelUnity.setTexture(2, manatusNewCloth);}貼圖混亂的原因與衣服制作的簡單說明
必須要保證新服裝的貼圖和以前的貼圖位置相同,這樣才能避免貼圖混亂。
按鈕音樂播放方法的封裝與音樂資源的賦值
//音樂播放private AudioSource audioSource;public AudioClip[] audioClips;首先在Awake中進行初始化
audioSource = GetComponent<AudioSource>();audioSource.loop = true;audioSource.clip = audioClips[0];audioSource.Play();添加Button點擊的音效
public void PlayButtonSound(){audioSource.PlayOneShot(audioClips[8]);}音樂添加的完成
完成音樂的添加。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的二次元日系游戏制作工具 - live2dSDK项目实战的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Windows 2008 R2 标准版
- 下一篇: 抖音 iOS 推荐 Feed 容器化总结
