Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)

原教程:siki:二次元日系游戏制作工具 - live2dSDK入门教程
Unity学习笔记—二次元日系游戏制作(理论篇)
Unity学习笔记—二次元日系游戏制作(实践篇-游戏初始化场景制作)

7、模型动作,表情的调用
Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第1张图片
翠花的动画与表情编号:
表情:1.正常 2.正常 3.不高兴 4.有点伤心 5.高兴 6.呆萌 7.害羞 8.无聊
tap_body:1.有什么事情吗 2.开心的有什么事情吗 3.请不要那样
pinch_out:抽风似的开心 pinch_in 今天辛苦了。 shake 吓死了

8、聊天

    public GameObject ChatUI;

    //点击聊天按钮,进入聊天界面
    public void ClickChatBtn()
    {
        actionBtns.SetActive(false);
        ChatUI.SetActive(true);
        if (favor>=100)
        {
            //StartMotion(string group, int no, int priority)
            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 = "谢谢啊!二狗子,你也很帅.....";
                    lAppModelProxy.GetModel().SetExpression("f08");
                }
                else
                {
                    ChangeFavor(2);
                    talkLineText.text = "哦,谢谢";
                    lAppModelProxy.GetModel().SetExpression("f08");
                }
                break;
            case 1:
                 if (favor>60)
                {
                    ChangeFavor(20);
                    talkLineText.text = "啊...哦....不好意思,谢谢哈....";
                    lAppModelProxy.GetModel().SetExpression("f05");
                }
                else
                {
                    ChangeFavor(-20);
                    talkLineText.text = "你看错了,你是残留的纸巾!刚才我不小心留下的,你真不礼貌!!";
                    lAppModelProxy.GetModel().SetExpression("f03");
                }
                break;
            case 2:
                 if (favor>100)
                {
                    ChangeFavor(40);
                    talkLineText.text = "那....咱们一起去吃饭,下午去哪玩....";
                    lAppModelProxy.GetModel().SetExpression("f07");
                }
                else
                {
                    ChangeFavor(-40);
                    talkLineText.text = "你这人说话怎么这样啊,我又没得罪你!!";
                    lAppModelProxy.GetModel().SetExpression("f04");
                }
                break;
            default:
                break;
        }
    }
    
     public void ResetUI()
    {
        workUI.SetActive(false);
        talkLine.SetActive(false);
        actionBtns.SetActive(true);
        lAppModelProxy.SetVisible(true);
        lAppModelProxy.GetModel().SetExpression("f01");
        leftdays--;

    }

Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第2张图片
Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第3张图片

9、约会

    public SpriteRenderer bgimage;
    public Sprite[] dateSprites;
    
     //触发约会按钮
    public void ClickDateBtn()
    {
        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
                {
                    ChangeFavor(-50);
                    talkLineText.text = "没事,不用在意,我最近零花钱比较多";
                }
                break;
            case 2:
                if (gold >= 150)
                {
                    ChangeGold(-150);
                    ChangeFavor(300);
                    talkLineText.text = "蟹黄汤包,烤鸭还有其他的甜品真的都太好吃了!" + "\n " + "谢谢招待,二狗子。";
                    hasEnoughGold = true;
                }
                else
                {
                    ChangeFavor(-150);
                    talkLineText.text = "下次有机会我再请你吃饭吧~";
                }
                break;
            case 3:
                if (gold >= 300)
                {
                    ChangeGold(-300);
                    ChangeFavor(500);
                    talkLineText.text = "今天真的很开心~" + "\n " + "还有谢谢你送的礼物,你人真好~";
                    hasEnoughGold = true;
                }
                else
                {
                    ChangeFavor(-300);
                    talkLineText.text = "那个娃娃真的好可爱哦~好想要....";
                }
                break;
            default:
                break;
        }
        if (hasEnoughGold)
        {
            lAppModelProxy.GetModel().StartMotion("pinch_in",0,2);
        }
        else
        {
            lAppModelProxy.GetModel().StartMotion("flick_head",0,2);
        }
    }
     public void ResetUI()
    {
        workUI.SetActive(false);
        talkLine.SetActive(false);
        actionBtns.SetActive(true);
        lAppModelProxy.SetVisible(true);
        lAppModelProxy.GetModel().SetExpression("f01");
        bgimage.sprite = dateSprites[0];
        leftdays--;

    }

Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第4张图片
Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第5张图片
10、表白

 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");
            gameOver = true;
        }
        else
        {
            talkLineText.text = "二狗子,你..你," + "\n " +
                "突然的表白吓我一跳" + "\n " +
                "你真的喜欢我是吗?"+ "\n " +
                "可是我们还不够了解彼此...";
            lAppModelProxy.GetModel().StartMotion("shake", 0, 2);
            lAppModelProxy.GetModel().SetExpression("f04");
        }
    }

三、Boss
Boss出现


    //GameManager
    //游戏逻辑判断
    public bool gameOver;


//Live2dSampleModel  
using live2d;
//Boss脚本
public class Live2dSampleModel : MonoBehaviour
{
    public TextAsset modeFile;
    public Texture2D texture;
    public TextAsset idelMotionFile;
    public GameObject woman;

    private Live2DModelUnity live2DModel;
    private Matrix4x4 live2DCanvasPos;

    private Live2DMotion live2DMotionIdle;
    private MotionQueueManager motionQueueManager;

    private EyeBlinkMotion eyeBlinkMotion;

    //判断当前Boss是否被打败
    private bool isDefeat;
    public float moveSpeed;

    private Vector3 initPos;

    private int hitCount;

    void Start()
    {
        Live2D.init();
        live2DModel = Live2DModelUnity.loadModel(modeFile.bytes);
        live2DModel.setTexture(0, texture);
        float modelWidth = live2DModel.getCanvasWidth();
        live2DCanvasPos=Matrix4x4.Ortho(0,modelWidth,modelWidth,0,-50.0f,50.0f);

        live2DMotionIdle = Live2DMotion.loadMotion(idelMotionFile.bytes);

        live2DMotionIdle.setLoop(true);

        motionQueueManager = new MotionQueueManager();
        eyeBlinkMotion = new EyeBlinkMotion();
        motionQueueManager.startMotion(live2DMotionIdle);

        initPos = transform.position;
    }

    void Update()
    {
        live2DModel.setMatrix(transform.localToWorldMatrix * live2DCanvasPos);
        motionQueueManager.updateParam(live2DModel);
        eyeBlinkMotion.setParam(live2DModel);
        
        live2DModel.update();

        if (GameManager.Instance.gameOver)
        {
            return;
        }
        //判断当前Boss是否追赶上小姐姐
        if ((woman.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);
        }
    }

    private void OnRenderObject()
    {
        live2DModel.draw();

    }

    //击打
    private void OnMouseDown()
    {
        if (GameManager.Instance.gameOver)
        {
            return;
        }
        if (hitCount>=20)
        {
            isDefeat = true;

        }
        else
        {
            hitCount++;

        }
    }
}  

Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第6张图片
小姐姐逃跑
LAppModelProxy:

    //躲避Boss
    public bool isRunningAway;

    public float moveSpeed;

    private Vector3 initPos;
    
    void Awake()
    {
        initPos = transform.position;
    }
    void 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)
        {
            //在GameManager中定义 public Live2dSampleModel badBoyScript;
            //在Live2dSampleModel中将isDefeat换为 public
            if (GameManager.Instance.badBoyScript.isDefeat)  
            {
                transform.position = Vector3.Lerp(transform.position, initPos, 0.1f);
            }
        }
	}

Boss逻辑判断

    public Live2dSampleModel badBoyScript;
    public GameObject badBoyTalkLine;
    
     public void ResetUI()
    {
        leftdays--;
        if (leftdays==5)
        {
            CreateBadBoy();
        }

    }

    //产生Boss
    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);
    }

 //GameManager
 public void  DefeatBadBoy()
    {
        lAppModelProxy.GetModel().StartMotion("shake", 0, 2);
        talkLine.SetActive(true);
        talkLineText.text = "刚刚吓死我了,谢谢你,二狗子"+"\n"+
            "要不是你救我,我就..."+"\n"+
            "你人好还用改,真帅...";
        ChangeFavor(300);
    }
   //Live2dSampleModel 
    private void OnMouseDown()
    {     
        if (hitCount>=20)
        {
            isDefeat = true;
            GameManager.Instance.DefeatBadBoy();
        }
    }

四、鼠标点击特效
Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第7张图片
Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第8张图片

    //GameManager
    //鼠标点击特效
    public GameObject clickEffect;
    public Canvas canvas;
    
    void 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;

        }

五、游戏结束逻辑

  public GameObject gameOverBtns;
   void Update()
    {
        //游戏结束逻辑
        if (gameOver)
        {
            talkLine.SetActive(true);
            gameOverBtns.SetActive(true);
            actionBtns.SetActive(false);
            if (favor>=1500)
            {
                talkLineText.text = "二狗子终于追到了村里最漂亮的女孩"+"\n "+
                    "最后他们幸福的在一起了。";
            }
            else if (leftdays!=0&&favor<1500)
            {
                talkLineText.text = "翠花受到欺负的时候二狗子没能保护她" + "\n " + "最后他们决裂了。";
            }
            else
            {
                talkLineText.text = "二狗子在翠花出国前没能获取她的芳心," + "\n " + 
                    "翠花出国留学了,他们最终没有在一起。";
            }
        }
        
    public void LoadScene(int sceneNum)
    {
        SceneManager.LoadScene(sceneNum);
    }

Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下)_第9张图片
六、给小姐姐换衣服

using UnityEngine.SceneManagement;
using live2d;
public Texture2D womanNewCloth;
public void ResetUI()
    {
        leftdays--;
        if (leftdays==5)
        {
            CreateBadBoy();
        }
        else if (leftdays==10)
        {
            Live2DModelUnity live2DModelUnity = lAppModelProxy.GetModel().GetLive2DModelUnity();
            live2DModelUnity.setTexture(2, womanNewCloth);
        }
        else if (leftdays==0)
        {
            gameOver = true;
        }

    }
        audioSource = GetComponent<AudioSource>();
        audioSource.loop = true;
        audioSource.clip = audioClicps[0];
        audioSource.Play();

你可能感兴趣的:(Unity学习笔记—二次元日系游戏制作(实践篇-游戏开始场景制作下))