• Unity—项目小技术总结


    目录

    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()方法

    1. public class LoadManager : MonoBehaviour
    2. {
    3.     public GameObject loadScreen;
    4.     public Slider slider;
    5.     public Text text;
    6.     public void LoadNextLevel()
    7.     {
    8.         StartCoroutine(Loadlevel());
    9.     }
    10.     IEnumerator Loadlevel()
    11.     {
    12.         loadScreen.SetActive(true);
    13.         AsyncOperation operation = SceneManager.LoadSceneAsync(8);
    14.         operation.allowSceneActivation = false;
    15. //异步加载到90%左右停住,防止加载完毕后直接切换场景
    16.         while (!operation.isDone)//isDone操作是否已完成
    17.         {
    18.             slider.value = operation.progress;//获取操作进度
    19.             text.text = operation.progress * 100 + "%";
    20.             if (operation.progress >= 0.9f)
    21.             {
    22.                 slider.value = 1;
    23.                 text.text = "按下任意按键,继续……";
    24.                 if (Input.anyKeyDown)
    25.                 {
    26.                     operation.allowSceneActivation = true;
    27. //允许在场景准备就绪后立即激活场景
    28.                 }
    29.             }
    30.             yield return null;
    31.         }
    32.     }
    33. }

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

    通过数组显示文本框

    新建对话框,文字

    新建空物体挂载DialogueManager_脚本

    1. public class DialogueManager_ : MonoBehaviour
    2. {
    3.     public static DialogueManager_ instance;//静态的DialogueManager_属性instance保证了它可以通过类访问,而不是通过实例访问
    4.     public GameObject dialogueBox_baimeiren;
    5.     public Text dialogueText;
    6.     [TextArea(1, 3)] public string[] dialogueLines;
    7.     [SerializeField] private int currentLine;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         dialogueText.text = dialogueLines[currentLine];
    12.     }
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         if (dialogueBox_.activeInHierarchy)
    17.         {
    18.             if (Input.GetMouseButtonUp(0))
    19.             {
    20.                 currentLine++;
    21.                 if (currentLine < dialogueLines.Length)
    22.                     dialogueText.text = dialogueLines[currentLine];
    23.                 else
    24.                     dialogueBox_.SetActive(false);
    25.             }
    26.         }
    27.     }
    28.     public void ShowDialogue(string[] _newLines)
    29.     {
    30.         dialogueLines = _newLines;
    31.         currentLine = 0;
    32.         dialogueText.text = dialogueLines[currentLine];
    33.         dialogueBox_.SetActive(true);
    34.     }
    35.     public void Awake()
    36.     {
    37.         instance = this;
    38.     }
    39. }

    触发地点挂载Talkable_

    1. public class Talkable_ : MonoBehaviour
    2. {
    3.     [SerializeField] private bool isEntered;
    4.     [TextArea(1, 3)] public string[] lines;
    5.     private void OnTriggerEnter(Collider other)
    6.     {
    7.         if (other.CompareTag("Player"))
    8.         {
    9.             isEntered = true;
    10.         }
    11.     }
    12.     private void OnTriggerExit(Collider other)
    13.     {
    14.         if (other.CompareTag("Player"))
    15.         {
    16.             isEntered = false;
    17.         }
    18.     }
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if (isEntered && Input.GetKeyDown(KeyCode.Q))
    23.         //if (isEntered)
    24.         {
    25.             DialogueManager_.instance.ShowDialogue(lines);
    26.         }
    27.     }
    28. }

    4.书翻页

    ——Book_Page Curl Pro2.0插件

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

    5.3D物体在画布上显示

    Canvas的Render Mode-Screen Space-Camera

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

    1. public class MouseRotate : MonoBehaviour
    2. {
    3.     public Transform m_target;
    4.     private bool isDown1;
    5.     private bool isDown0;
    6.     void Start()
    7.     {
    8.         isDown1 = false;
    9.         isDown0 = false;
    10.     }   
    11.     void Update()
    12.     {
    13.         // 按下了鼠标中键
    14.         if (Input.GetMouseButtonDown(1))
    15.             isDown1 = true;
    16.         // 抬起了鼠标中键
    17.         if (Input.GetMouseButtonUp(1))
    18.             isDown1 = false;
    19.         // 按下鼠标右键
    20.         if (Input.GetMouseButtonDown(0))
    21.             isDown0 = true;
    22.         // 抬起了鼠标右键
    23.         if (Input.GetMouseButtonUp(0))
    24.             isDown0 = false;
    25.         // 缩放 Input.mouseScrollDelta.y当前的鼠标滚动增量
    26.         m_target.localScale += Time.deltaTime * m_target.localScale * Input.mouseScrollDelta.y*8;
    27.     }
    28.     private void LateUpdate()
    29.     {
    30.         if (isDown1)
    31.         {
    32.             // 计算单位时间内鼠标的偏移量
    33.             float x = Input.GetAxis("Mouse X");
    34.             float y = Input.GetAxis("Mouse Y");
    35.             Vector3 rot = new Vector3(y, -x, 0);
    36.             float spd = 10; //速度
    37.             m_target.Rotate(spd * rot, Space.World);
    38.         }
    39.         if (isDown0)
    40.         {
    41.             // 计算单位时间内鼠标的偏移量
    42.             float x = Input.GetAxis("Mouse X");
    43.             float y = Input.GetAxis("Mouse Y");
    44.             float spd = 0.4f;   //速度
    45.             m_target.Translate(spd * new Vector3(x, y), Space.World);
    46.         }
    47.     }
    48. }

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

    1. public class SilderMenuAnim : MonoBehaviour
    2. {
    3.     public GameObject PanelMenu;
    4.     public void ShowHideMenu()
    5.     {
    6.         if (PanelMenu != null)
    7.         {
    8.             Animator animator = PanelMenu.GetComponent();
    9.             if(animator!=null)
    10.             {
    11.                 bool isOpen = animator.GetBool("show");
    12.                 animator.SetBool("show", !isOpen);
    13.             }
    14.         }
    15.     }
    16. }

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

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

    1. public class playerposition : MonoBehaviour
    2. {
    3.     public float PlayerPosX = 0.0f, PlayerPosY = 0.0f, PlayerPosZ = 0.0f;
    4.     void Start()
    5.     {
    6.         PlayerPosX = PlayerPrefs.GetFloat("playerPosX");
    7.         PlayerPosY = PlayerPrefs.GetFloat("playerPosY");
    8.         PlayerPosZ = PlayerPrefs.GetFloat("playerPosZ");
    9.         if (PlayerPosX == 0.0f)
    10.         {
    11.             gameObject.transform.position = new Vector3(0, 0.2f, 0f);
    12.         }
    13.         if (PlayerPosX != 0.0f)
    14.         {
    15.             gameObject.transform.position = new Vector3(PlayerPosX, PlayerPosY, PlayerPosZ);
    16.         }
    17. }

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

    1. public class Into: MonoBehaviour
    2. {
    3.     public GameObject text;
    4.     public GameObject player;
    5.     private void OnTriggerStay(Collider other)
    6.     {      
    7.         if (other.CompareTag("Player") && Input.GetKeyDown(KeyCode.Z))
    8.         {       
    9.             SceneManager.LoadSceneAsync(3);
    10.             text.gameObject.SetActive(true);
    11.             PlayerPrefs.SetFloat("playerPosX", player.transform.position.x);
    12.             PlayerPrefs.SetFloat("playerPosY", player.transform.position.y);
    13.             PlayerPrefs.SetFloat("playerPosZ", player.transform.position.z);
    14.         }
    15.     }
    16. }

  • 相关阅读:
    好用的翻译插件-一键自动翻译插件软件
    嵌入式 单片机面试 通信协议常见问题答案 串口通信 IIC通信 SPI通信 协议解析讲解 RS232 RS485 协议 IIC总线
    基于html5的网上书店系统
    【设备布局】基于粒子群优化算法的设备布局设计研究(Matlab代码实现)
    【算法】字典序
    听GPT 讲Rust源代码--library/std(16)
    oracle %ROWTYPE使用方法
    竞赛 基于卷积神经网络的乳腺癌分类 深度学习 医学图像
    基于微信JAVA后台校园小程序系统设计与实现 开题报告
    零基础也能做Apple大片!这款免费工具帮你渲染、做特效、丝滑展示
  • 原文地址:https://blog.csdn.net/m0_63330263/article/details/127694808