欢迎登录

unity搭建房子顺序提示和位置提示

:2018-07-31    :593 次   :19021 字   

工程说明:


房子有地基,有四根柱子,有窗户,有屋顶,有步骤提示,点击step后,开始搭建,开始步骤一,地基高亮,点击地基可以自动归位,然后进去步骤二,柱子高亮,四根柱子可以随意顺序搭建,搭建完,进入步骤三,可以选择安装窗户,不安装可以step下一步,固定的房子搭建顺序,只有高亮的部件点击才会发生位置归位,搭建完提示消失。

图示:

使用教程:

1.先看看有什么部件新建




2.步骤提示的窗口需要增加ui的text部件


//更改步骤文字的代码
GetComponent<Text>().text = "第一步:\n安装底座。\n(高亮部分对接)";


3.位置高亮代码可以参考这篇文章

https://blog.xygeng.cn/?id=77


4.搭建过程视角的转变参考这篇文章

https://blog.xygeng.cn/?id=65


5.部件点击后自动到达指定位置,参考这片文章

https://blog.xygeng.cn/?id=82


6.主要代码都是挂在按钮step上,通过判断鼠标点击和步骤来操作各个组件的移动和高亮

代码一:步骤提示和高亮位置提示


using UnityEngine.UI;
using UnityEngine;
using HighlightingSystem;
public class Step : MonoBehaviour {
    // Use this for initialization
    public RectTransform txt;//2dUI
    public int step;
    void Start () {
        step = 0;
        Button btn = this.GetComponent<Button>();
        btn.onClick.AddListener(OnClick);
    }
// Update is called once per frame
void Update () {
}
    private void OnClick()
    {
        step++;
        Debug.Log("点击了");
        run();
    }
    void run()
    {
        //判断步骤改变位置提示,顺序提示
        switch (step)
        {
            case 1: Step1(); break;
            case 2: Step2(); break;
            case 3: Step3(); break;
            case 4: Step4(); break;
            case 5: Step5(); break;
        }
        if (step == 5)
        {
            step = 0;
        }
    }
    //设置高亮
    void GetHightLightning(string name)
    {
        GameObject.Find(name).AddComponent<highlight>();
    }
    //取消高亮
    void RemoveHightLightning(string name)
    {
        Destroy(GameObject.Find(name).gameObject.GetComponent<highlight>());
        Destroy(GameObject.Find(name).gameObject.GetComponent<Highlighter>());
    }
    //步骤一
    void Step1()
    {
        GetHightLightning("Base");
        txt.GetComponent<Text>().text = "第一步:\n安装底座。\n(高亮部分对接)";
    }
    //步骤二
    void Step2()
    {
        RemoveHightLightning("Base");
        GetHightLightning("_bar1"); GetHightLightning("base_1");
        GetHightLightning("_bar2"); GetHightLightning("base_2");
        GetHightLightning("_bar3"); GetHightLightning("base_3");
        GetHightLightning("_bar4"); GetHightLightning("base_4");
        txt.GetComponent<Text>().text = "第二步:\n在底座安装四个柱子。\n(高亮部分对接)";
    }
    //步骤三
    void Step3()
    {
        RemoveHightLightning("base_1"); RemoveHightLightning("_bar1");
        RemoveHightLightning("base_2"); RemoveHightLightning("_bar2");
        RemoveHightLightning("base_3"); RemoveHightLightning("_bar3");
        RemoveHightLightning("base_4"); RemoveHightLightning("_bar4");
        txt.GetComponent<Text>().text = "第三步:\n可以选择在柱子上安装窗户,不安装可以跳过安装,默认安装。\n(高亮部分对接)";
        GetHightLightning("Bar3"); GetHightLightning("Bar4");
        GetHightLightning("window");
    }
    void Step4()
    {
        RemoveHightLightning("Bar3"); RemoveHightLightning("Bar4"); RemoveHightLightning("window");
        GetHightLightning("_roof1"); GetHightLightning("_roof2"); GetHightLightning("_roof3"); GetHightLightning("_roof4"); GetHightLightning("roof");
        txt.GetComponent<Text>().text = "第四步:\n在四个柱子上对接屋顶。\n(高亮部分对接)";
    }
    void Step5()
    {
        RemoveHightLightning("_roof1"); RemoveHightLightning("_roof2"); RemoveHightLightning("_roof3");
        RemoveHightLightning("_roof4");
        RemoveHightLightning("roof");
        txt.GetComponent<Text>().text = "第五步:\n房子搭建完成。";
    }
    public int intStep()
    {
        return step;
    }
    public void setStep(int s)
    {
        step = s;
        run();
    }
}


代码二:控制部件位置转换和步骤的进行


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateHouse : MonoBehaviour {
    // Use this for initialization
    public int step;
    public int bar;
void Start () {
        step = 0;bar = 0;
    }
    // Update is called once per frame
    void Update()
    {
        step = this.gameObject.GetComponent<Step>().intStep();
        if (bar == 4)
        {
              step=3;GetComponent<Step>().setStep(step);
            bar = 0;
        }
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                GameObject gameObj = hitInfo.collider.gameObject;
                //判断点击了哪个部件,执行哪个方法
                if(gameObj.gameObject.name.Equals("Base"))
                {
                    Base(gameObj);
                }
                else if(gameObj.gameObject.name.Equals("Bar1"))
                {
                    Bar1(gameObj);
                }
                else if (gameObj.gameObject.name.Equals("Bar2"))
                {
                    Bar2(gameObj);
                }
                else if (gameObj.gameObject.name.Equals("Bar3"))
                {
                    Bar3(gameObj);
                }
                else if (gameObj.gameObject.name.Equals("Bar4"))
                {
                    Bar4(gameObj);
                }
                else if (gameObj.gameObject.tag.Equals("window"))
                {
                    window(GameObject.Find("window"));
                }
                else if (gameObj.gameObject.tag.Equals("roof"))
                {
                    roof(GameObject.Find("roof"));
                }
            }
        }
    }
    //地基函数
    void Base(GameObject OBJ)
    {
        if (step == 1)
        {
            Debug.Log("点击了地基");
            OBJ.gameObject.transform.position = new Vector3(0, 10, 0);//实际(0,10,0)
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            step = 2;
            GetComponent<Step>().setStep(step);
        }
    }
    void Bar1(GameObject OBJ)
    {
        if (step == 2)
        {
            Debug.Log("点击了柱子");
            OBJ.gameObject.transform.position = new Vector3(-20, 36, -20);//实际(-20, 36, -20)
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            bar++;
        }
    }
    void Bar2(GameObject OBJ)
    {
        if (step == 2)
        {
            Debug.Log("点击了柱子");
            OBJ.gameObject.transform.position = new Vector3(20,36, -20);//实际(20,36, -20)
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            bar++;
        }
    }
    void Bar3(GameObject OBJ)
    {
        if (step == 2)
        {
            Debug.Log("点击了柱子");
            OBJ.gameObject.transform.position = new Vector3(20, 36, 20);//实际
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            bar++;
        }
    }
    void Bar4(GameObject OBJ)
    {
        if (step == 2)
        {
            Debug.Log("点击了柱子");
            OBJ.gameObject.transform.position = new Vector3(-20, 36, 20);//实际
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            bar++;
        }
    }
    void window(GameObject OBJ)
    {
        if (step == 3)
        {
            Debug.Log("点击了窗户");
            OBJ.gameObject.transform.position = new Vector3(0, 50, 20);//实际(0,33,20)
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            step ++;
            GetComponent<Step>().setStep(step);
        }
    }
    public void roof(GameObject OBJ)
    {
        if (step ==4)
        {
            Debug.Log("点击了屋顶");
            OBJ.gameObject.transform.position = new Vector3(0, 70, 0);//实际(0,63,0)
            OBJ.gameObject.transform.Rotate(0, 0, 0);
            step = 5;
            GetComponent<Step>().setStep(step);
        }
    }
}
工程包大小:10.8m | 来源:百度网盘
已经过安全软件检测无毒,请您放心下载。
    暂无评论,快来抢沙发吧!
×