Unity—项目小技术总结

时间:2022-11-06 08:58:16

目录

1.将视频做背景播放

2.含进度条的异步加载场景

3.进入触发,显示简单对话系统

4.书翻页

5.3D物体在画布上显示

6.移动、旋转、缩放物体

7.动画显示与隐藏菜单、卷轴等

8.使用PlayerPrefs切换场景时保存和读取人物位置


1.将视频做背景播放

空物体上挂载Video Player组件

Render Mode改为Camera Far Plane

2.含进度条的异步加载场景

Unity中使用协程搭配异步写代码更佳,例如通过异步加载场景,在加载过程中通过每帧更新进度条给予玩家一个反馈,可以在协程中调用异步加载场景的方法,然后暂停协程,等待当前一帧结束,然后更新进度条,反复之后,就完成进度条显示进度加载场景效果

制作步骤:

①做一个Panel,放背景

②画布上放Slider(Handle修改进度条图片,Fill改颜色)

③Text显示文字

④编写脚本  切换场景按钮On Click() 事件调用LoadNextLevel()方法

public class LoadManager : MonoBehaviour

{

    public GameObject loadScreen;

    public Slider slider;

    public Text text;

    public void LoadNextLevel()

    {

        StartCoroutine(Loadlevel());

    }

    IEnumerator Loadlevel()

    {

        loadScreen.SetActive(true);

        AsyncOperation operation = SceneManager.LoadSceneAsync(8);

        operation.allowSceneActivation = false;

//异步加载到90%左右停住,防止加载完毕后直接切换场景

        while (!operation.isDone)//isDone操作是否已完成

        {

            slider.value = operation.progress;//获取操作进度

            text.text = operation.progress * 100 + "%";

            if (operation.progress >= 0.9f)

            {

                slider.value = 1;

                text.text = "按下任意按键,继续……";

                if (Input.anyKeyDown)

                {

                    operation.allowSceneActivation = true;

//允许在场景准备就绪后立即激活场景

                }

            }

            yield return null;

        }

    }

}

3.进入触发,显示简单对话系统

通过数组显示文本框

新建对话框,文字

新建空物体挂载DialogueManager_脚本

public class DialogueManager_ : MonoBehaviour

{

    public static DialogueManager_ instance;//静态的DialogueManager_属性instance保证了它可以通过类访问,而不是通过实例访问

    public GameObject dialogueBox_baimeiren;

    public Text dialogueText;

    [TextArea(1, 3)] public string[] dialogueLines;

    [SerializeField] private int currentLine;

    // Start is called before the first frame update

    void Start()

    {

        dialogueText.text = dialogueLines[currentLine];

    }



    // Update is called once per frame

    void Update()

    {

        if (dialogueBox_.activeInHierarchy)

        {

            if (Input.GetMouseButtonUp(0))

            {



                currentLine++;

                if (currentLine < dialogueLines.Length)

                    dialogueText.text = dialogueLines[currentLine];

                else

                    dialogueBox_.SetActive(false);

            }

        }

    }

    public void ShowDialogue(string[] _newLines)

    {

        dialogueLines = _newLines;

        currentLine = 0;

        dialogueText.text = dialogueLines[currentLine];

        dialogueBox_.SetActive(true);

    }

    public void Awake()

    {

        instance = this;

    }

}

触发地点挂载Talkable_

public class Talkable_ : MonoBehaviour

{

    [SerializeField] private bool isEntered;

    [TextArea(1, 3)] public string[] lines;

    private void OnTriggerEnter(Collider other)

    {

        if (other.CompareTag("Player"))

        {

            isEntered = true;

        }

    }

    private void OnTriggerExit(Collider other)

    {

        if (other.CompareTag("Player"))

        {

            isEntered = false;

        }

    }

    // Update is called once per frame

    void Update()

    {

        if (isEntered && Input.GetKeyDown(KeyCode.Q))

        //if (isEntered)

        {

            DialogueManager_.instance.ShowDialogue(lines);

        }

    }

}

4.书翻页

——Book_Page Curl Pro2.0插件

BookPro里Papers添加页数,对应每页修改其图片

5.3D物体在画布上显示

Canvas的Render Mode-Screen Space-Camera

6.移动、旋转、缩放物体

public class MouseRotate : MonoBehaviour

{

    public Transform m_target;

    private bool isDown1;

    private bool isDown0;

    void Start()

    {

        isDown1 = false;

        isDown0 = false;

    }   

    void Update()

    {

        // 按下了鼠标中键

        if (Input.GetMouseButtonDown(1))

            isDown1 = true;

        // 抬起了鼠标中键

        if (Input.GetMouseButtonUp(1))

            isDown1 = false;

        // 按下鼠标右键

        if (Input.GetMouseButtonDown(0))

            isDown0 = true;

        // 抬起了鼠标右键

        if (Input.GetMouseButtonUp(0))

            isDown0 = false;

        // 缩放 Input.mouseScrollDelta.y当前的鼠标滚动增量

        m_target.localScale += Time.deltaTime * m_target.localScale * Input.mouseScrollDelta.y*8;

    }

    private void LateUpdate()

    {

        if (isDown1)

        {

            // 计算单位时间内鼠标的偏移量

            float x = Input.GetAxis("Mouse X");

            float y = Input.GetAxis("Mouse Y");

            Vector3 rot = new Vector3(y, -x, 0);

            float spd = 10; //速度

            m_target.Rotate(spd * rot, Space.World);

        }

        if (isDown0)

        {

            // 计算单位时间内鼠标的偏移量

            float x = Input.GetAxis("Mouse X");

            float y = Input.GetAxis("Mouse Y");

            float spd = 0.4f;   //速度

            m_target.Translate(spd * new Vector3(x, y), Space.World);

        }

    }

}

7.动画显示与隐藏菜单、卷轴等

public class SilderMenuAnim : MonoBehaviour

{

    public GameObject PanelMenu;

    public void ShowHideMenu()

    {

        if (PanelMenu != null)

        {

            Animator animator = PanelMenu.GetComponent<Animator>();

            if(animator!=null)

            {

                bool isOpen = animator.GetBool("show");

                animator.SetBool("show", !isOpen);

            }

        }

    }

}

8.使用PlayerPrefs切换场景时保存和读取人物位置

挂载在第三人称上,以下脚本:

public class playerposition : MonoBehaviour

{

    public float PlayerPosX = 0.0f, PlayerPosY = 0.0f, PlayerPosZ = 0.0f;

    void Start()

    {

        PlayerPosX = PlayerPrefs.GetFloat("playerPosX");

        PlayerPosY = PlayerPrefs.GetFloat("playerPosY");

        PlayerPosZ = PlayerPrefs.GetFloat("playerPosZ");

        if (PlayerPosX == 0.0f)

        {

            gameObject.transform.position = new Vector3(0, 0.2f, 0f);

        }

        if (PlayerPosX != 0.0f)

        {

            gameObject.transform.position = new Vector3(PlayerPosX, PlayerPosY, PlayerPosZ);

        }

}

挂载在物体上,以下脚本:

public class Into: MonoBehaviour

{

    public GameObject text;

    public GameObject player;

    private void OnTriggerStay(Collider other)

    {      

        if (other.CompareTag("Player") && Input.GetKeyDown(KeyCode.Z))

        {       

            SceneManager.LoadSceneAsync(3);

            text.gameObject.SetActive(true);

            PlayerPrefs.SetFloat("playerPosX", player.transform.position.x);

            PlayerPrefs.SetFloat("playerPosY", player.transform.position.y);

            PlayerPrefs.SetFloat("playerPosZ", player.transform.position.z);

        }

    }

}