1.仿制Maya参考网格
using UnityEngine;public class ReferenceGridRenderer : MonoBehaviour
{//[SerializeField] Color color = new Color(1, 1, 1, 0.5f);[SerializeField] [Range(1, 1000)] float range = 20;[SerializeField] [Range(0.1f, 100)] float interval = 1;[SerializeField] float centerLineWidth = 0.05f;[SerializeField] float outerLineWidth = 0.01f;[SerializeField] Color centerLineColor = Color.white;[SerializeField] Color outerLineColor = new Color(1, 1, 1, 0.5f);void Start(){CreateReferenceGrid();}void CreateReferenceGrid(){GameObject referenceGrid = new GameObject("Reference Grid");for (float i = -range; i <= range; i += interval){GameObject lineObjectX = new GameObject("Line X");lineObjectX.transform.SetParent(referenceGrid.transform);LineRenderer lineRendererX = lineObjectX.AddComponent<LineRenderer>();lineRendererX.material = new Material(Shader.Find("Sprites/Default"));lineRendererX.startColor = (i == 0) ? centerLineColor : outerLineColor;lineRendererX.endColor = (i == 0) ? centerLineColor : outerLineColor;lineRendererX.startWidth = (i == 0) ? centerLineWidth : outerLineWidth;lineRendererX.endWidth = (i == 0) ? centerLineWidth : outerLineWidth;GameObject lineObjectZ = new GameObject("Line Z");lineObjectZ.transform.SetParent(referenceGrid.transform);LineRenderer lineRendererZ = lineObjectZ.AddComponent<LineRenderer>();lineRendererZ.material = new Material(Shader.Find("Sprites/Default"));lineRendererZ.startColor = (i == 0) ? centerLineColor : outerLineColor;lineRendererZ.endColor = (i == 0) ? centerLineColor : outerLineColor;lineRendererZ.startWidth = (i == 0) ? centerLineWidth : outerLineWidth;lineRendererZ.endWidth = (i == 0) ? centerLineWidth : outerLineWidth;lineRendererX.SetPositions(new Vector3[] { new Vector3(-range, 0, i), new Vector3(range, 0, i) });lineRendererZ.SetPositions(new Vector3[] { new Vector3(i, 0, -range), new Vector3(i, 0, range) });}}
}
2.仿制 ThreeEditor 参考网格
using UnityEngine;public class ReferenceGridRenderer : MonoBehaviour
{[SerializeField] [Range(1, 1000)] float range = 20;[SerializeField] [Range(0.1f, 100)] float interval = 1;[SerializeField] float centerLineWidth = 0.05f;[SerializeField] float outerLineWidth = 0.01f;[SerializeField] Color centerLineColor = Color.white;[SerializeField] Color outerLineColor = new Color(1, 1, 1, 0.5f);[SerializeField] float fifthLineWidth = 0.02f;[SerializeField] Color fifthLineColor = new Color(1, 0, 0, 0.5f);void Start(){CreateReferenceGrid();}void CreateReferenceGrid(){GameObject referenceGrid = new GameObject("Reference Grid");int count = 0;for (float i = -range; i <= range; i += interval){GameObject lineObjectX = new GameObject("Line X");lineObjectX.transform.SetParent(referenceGrid.transform);LineRenderer lineRendererX = lineObjectX.AddComponent<LineRenderer>();lineRendererX.material = new Material(Shader.Find("Sprites/Default"));if (i == 0){lineRendererX.startColor = centerLineColor;lineRendererX.endColor = centerLineColor;lineRendererX.startWidth = centerLineWidth;lineRendererX.endWidth = centerLineWidth;}else if (count % 5 == 0){lineRendererX.startColor = fifthLineColor;lineRendererX.endColor = fifthLineColor;lineRendererX.startWidth = fifthLineWidth;lineRendererX.endWidth = fifthLineWidth;}else{lineRendererX.startColor = outerLineColor;lineRendererX.endColor = outerLineColor;lineRendererX.startWidth = outerLineWidth;lineRendererX.endWidth = outerLineWidth;}GameObject lineObjectZ = new GameObject("Line Z");lineObjectZ.transform.SetParent(referenceGrid.transform);LineRenderer lineRendererZ = lineObjectZ.AddComponent<LineRenderer>();lineRendererZ.material = new Material(Shader.Find("Sprites/Default"));if (i == 0){lineRendererZ.startColor = centerLineColor;lineRendererZ.endColor = centerLineColor;lineRendererZ.startWidth = centerLineWidth;lineRendererZ.endWidth = centerLineWidth;}else if (count % 5 == 0){lineRendererZ.startColor = fifthLineColor;lineRendererZ.endColor = fifthLineColor;lineRendererZ.startWidth = fifthLineWidth;lineRendererZ.endWidth = fifthLineWidth;}else{lineRendererZ.startColor = outerLineColor;lineRendererZ.endColor = outerLineColor;lineRendererZ.startWidth = outerLineWidth;lineRendererZ.endWidth = outerLineWidth;}lineRendererX.SetPositions(new Vector3[] { new Vector3(-range, 0, i), new Vector3(range, 0, i) });lineRendererZ.SetPositions(new Vector3[] { new Vector3(i, 0, -range), new Vector3(i, 0, range) });count++;}}
}