系列文章目錄
unity工具
文章目錄
- 系列文章目錄
- 👉前言
- 👉一、Unity距離測量
- 1-1 制作預制體
- 1-2 編寫測量的腳本
- 👉二、鼠標點擊模型進行測量
- 👉二、字體面向攝像機的方法
- 👉二、最短距離測量方法
- 👉三、壁紙分享
- 👉總結
👉前言
有時候會用到測量距離的問題,所以寫了一個測量的小工具,方便使用,簡單記錄一下
大家好,我是心疼你的一切,不定時更新Unity開發技巧,覺得有用記得一鍵三連哦。
歡迎點贊評論哦.
下面就讓我們進入正文吧 !
提示:以下是本篇文章正文內容,下面案例可供參考
效果展示
測量距離
👉一、Unity距離測量
1-1 制作預制體
創建一個空物體,空物體下面創建兩個小球并設置一下大小,接著創建一個3D字體在空物體下面,最后在空物體上面添加LineRenderer組件
創建好的結構如下
st和ed是小球,這里切記小球不能帶碰撞盒,帶碰撞盒就會出現意外的情況 tm是3d字體
linerenderer組件設置一下Positions的數量為2,要么就在代碼里面設置為2
具體怎么設置就看你心情嘍
1-2 編寫測量的腳本
腳步掛載到剛剛創建的空物體上面即可
using UnityEngine;
using TMPro;//距離單位
public enum UnitType
{ mm = 1000, //毫米cm = 100, //厘米dm = 10, //分米m = 1, //米
}//[ExecuteInEditMode]
public class Line : MonoBehaviour
{public GameObject StObj, EdObj;TextMesh tm;LineRenderer line;[Header("實時繪制(較多會卡頓)")]public bool IsRt = false;[Header("線的粗細")]public float LineWidth = 0.05f;Material LineMat;[Header("線的顏色")]public Color LineColor;[Header("長度單位")]public UnitType unittype;Transform tram;private void Start(){LineMat = new Material(Shader.Find("Standard"));CreateTm();CreateLine();}void CreateTm(){tram = transform.Find("tm");if (tram != null)tm = tram.GetComponent<TextMesh>();if (tm == null){tm = new GameObject("tm").AddComponent<TextMesh>();tm.color = Color.white;tm.fontSize = 4;tm.transform.SetParent(this.transform);//tm.GetComponent<RectTransform>().sizeDelta = new Vector2(2, 1);//tm.alignment = TextAlignmentOptions.Center;}}void CreateLine(){line = gameObject.GetComponent<LineRenderer>();if (line == null)line = gameObject.AddComponent<LineRenderer>();line.material = LineMat;}public void DrawLineInfo(){tm.text = (Vector3.Distance(StObj.transform.position, EdObj.transform.position) * (int)unittype).ToString("F1") + unittype;tm.transform.position = (StObj.transform.position + EdObj.transform.position) / 2+new Vector3(0,0.1f,0);line.SetPositions(new Vector3[] { StObj.transform.position, EdObj.transform.position });line.startWidth = LineWidth;line.endWidth = LineWidth;LineMat.color = LineColor;}void Update(){if (IsRt)DrawLineInfo();}
}
👉二、鼠標點擊模型進行測量
新建一個腳本進行編寫
代碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;//測量距離
public class RangeFinding : MonoBehaviour
{//總控制public bool isClbool;private Vector3 posOne, posTwo;//測量控制public bool isOpenDistance;private int distanceInt; //計數控制public Transform prefabTransform; //測量的預制體private Transform myDistanceObj;public Transform allCLParentTransform; //所有預制體生成的父節點// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){if (isClbool){if (Input.GetMouseButtonDown(0)){posOne = Input.mousePosition;}Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;//距離if (isOpenDistance){if (Input.GetMouseButtonUp(0)){posTwo = Input.mousePosition;if (Physics.Raycast(ray, out hit, 1000) && posOne == posTwo){if (distanceInt == 0){distanceInt++;//鼠標點擊克隆物體myDistanceObj = Instantiate(prefabTransform, allCLParentTransform);// transform.TransformPoint(Prefab,hit.poit, Quaternion.identity);myDistanceObj.transform.GetChild(0).position = hit.point;}else{myDistanceObj.transform.GetChild(1).position = hit.point;//isOpenJL = false;distanceInt = 0;}}}if (distanceInt > 0){if (Physics.Raycast(ray, out hit, 1000)){myDistanceObj.transform.GetChild(1).position = hit.point;}}}}}
}
腳本隨便掛載,你開心就好
掛載完畢運行測試即可,把兩個bool值勾選上就可以進行測量了
運行結果,上面我已經放過了,就在放一下吧
測量距離
👉二、字體面向攝像機的方法
如果生成的距離字體不面向攝像機的話,需要加一下面向攝像機的方法,要不然沒有感覺
代碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class LookAtCamera : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.LookAt(Camera.main.transform);}
}
此代碼掛載到我們一開始創建的tm上面
👉二、最短距離測量方法
還是用到上面的預制體,其他不用改
廢話不多說了直接上代碼 代碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 最短距離 垂直距離
/// </summary>
public class MakeBeelineController : MonoBehaviour
{public bool isClbool;public bool isOpenMDistance;private Vector3 posOne, posTwo;private int distanceInt; //記錄次數public Transform prefabTransform; //測量的預制體private Transform myDistanceObj;public Transform allCLParentTransform; //所有預制體生成的父節點// Start is called before the first frame updatevoid Start(){}public void OpenCLLLLLL(){isClbool = true;isOpenMDistance = true;}public void CloseCLLLLLL(){isClbool = false;isOpenMDistance = false;if (allCLParentTransform.childCount == 0) return;if (allCLParentTransform.childCount > 0){for (int i = 0; i < allCLParentTransform.childCount; i++){Destroy(allCLParentTransform.GetChild(i).gameObject);}}}// Update is called once per framevoid Update(){if (isClbool){if (Input.GetMouseButtonDown(0)){posOne = Input.mousePosition;}Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit,hit1;//最短距離if (isOpenMDistance){if (Input.GetMouseButtonUp(0)){posTwo = Input.mousePosition;if (Physics.Raycast(ray, out hit) && posOne == posTwo){myDistanceObj = Instantiate(prefabTransform, allCLParentTransform);myDistanceObj.transform.GetChild(0).position = hit.point;Vector3 fwd = Vector3.down; // myDistanceObj.transform.GetChild(0).TransformDirection(Vector3.down);if (Physics.Raycast(myDistanceObj.transform.GetChild(0).position, fwd, out hit1, 1000)){myDistanceObj.transform.GetChild(1).position = hit1.point;}else{Destroy(myDistanceObj.gameObject);}}}}}}
}
場景掛載的示例圖如下
到此距離測量的方法已經結束了,如有其他需要或疑問,請留言評論即可,如需要其他的功能請自行修改添加擴展哦,愛你們么么噠
👉三、壁紙分享
下一篇文章分享關于面積的測量
👉總結
本次總結的就是測量距離的實現,有需要會繼續添加新的
如能幫助到你,就幫忙點個贊吧,三連更好哦,謝謝
你的點贊就是對博主的支持,有問題記得留言評論哦!
不定時更新Unity開發技巧,覺得有用記得一鍵三連哦。么么噠