本文實例為大家分享了unity實現屏幕上寫字效果的具體代碼,供大家參考,具體內容如下
先建立一個RawImage,然后再在這個圖片上加個LineRenderer組件,再建個材質球,把材質球的Shader改成Particles/Additive,把材質球拖給LineRenderer組件的Materials/Element 0(不拖也可以),最后再把代碼拖給空物體即可,代碼的Target是RawImage,下面的代碼
using System.Collections;using System.Collections.Generic;using UnityEngine;public class huaxian : MonoBehaviour {private GameObject clone;private LineRenderer line;int i;//帶有LineRender物體public GameObject target;void Start(){ Debug.Log("請開始寫字");} // Update is called once per frame void Update(){ if (Input.GetMouseButtonDown(0)) { //實例化對象 clone = (GameObject)Instantiate(target, target.transform.position, Quaternion.identity); //獲得該物體上的LineRender組件 line = clone.GetComponent<LineRenderer>(); //設置起始和結束的顏色 line.SetColors(Color.red, Color.blue); //設置起始和結束的寬度 line.SetWidth(0.2f, 0.1f); //計數 i = 0; } if (Input.GetMouseButton(0)) { //每一幀檢測,按下鼠標的時間越長,計數越多 i++; //設置頂點數 line.SetVertexCount(i); //設置頂點位置(頂點的索引,將鼠標點擊的屏幕坐標轉換為世界坐標) line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15))); } } }以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答