一個小的幫助腳本,用于在運行時在場景視圖和游戲視圖中為盒子碰撞器著色,即使沒有選擇帶有碰撞器的游戲物件。
我要添加的問題或另一個功能是在列舉中選擇模式 DrawOnBoth 時,它將在場景視圖視窗中以紅色繪制,在游戲視圖視窗中以綠色繪制。
現在,當模式為 DrawOnBoth 時,Windows 場景視圖和游戲視圖都以綠色繪制。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DrawOnBoxCollider : MonoBehaviour
{
public enum DrawStates
{
DrawOnGizmosRuntime,
DrawOnGameviewRuntime,
DrawOnBoth
}
public DrawStates drawingStates;
public Transform boxColliderToDrawOn;
private List<LineRenderer> lines = new List<LineRenderer>();
private BoxCollider boxCollider = null;
private bool drawOnGizmos = false;
private LineRenderer line;
private bool drawOnce = true;
// Start is called before the first frame update
void Start()
{
if (boxColliderToDrawOn != null)
{
boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
}
}
// Update is called once per frame
void Update()
{
if (boxColliderToDrawOn != null)
{
switch (drawingStates)
{
case DrawStates.DrawOnGameviewRuntime:
if (line != null)
{
line.enabled = true;
}
drawOnGizmos = false;
if (drawOnce)
{
if (boxCollider == null)
{
boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
}
DrawOnGameViewRuntime();
drawOnce = false;
drawOnGizmos = false;
}
break;
case DrawStates.DrawOnGizmosRuntime:
drawOnce = true;
if (line != null && drawOnGizmos == false)
{
var drawns = transform.GetComponentsInChildren<Transform>().Skip(1);
if (drawns.Count() > 0)
{
foreach (Transform drawn in drawns)
{
Destroy(drawn.gameObject);
}
}
}
if (boxCollider == null)
{
boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
}
drawOnGizmos = true;
break;
case DrawStates.DrawOnBoth:
if (line != null)
{
line.enabled = true;
}
if (boxCollider == null)
{
boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
}
if(drawOnce)
{
DrawOnGameViewRuntime();
drawOnce = false;
drawOnGizmos = true;
}
break;
}
}
}
private void DrawGizmosOnRunTime(Color color)
{
if (boxCollider != null && drawOnGizmos)
{
Gizmos.color = color;
Matrix4x4 rotationMatrix = Matrix4x4.TRS(boxCollider.transform.position, boxCollider.transform.rotation, boxCollider.transform.lossyScale);
Gizmos.matrix = rotationMatrix;
Gizmos.DrawWireCube(boxCollider.center, boxCollider.size);
}
}
private void OnDrawGizmos()
{
DrawGizmosOnRunTime(Color.red);
}
private void DrawOnGameViewRuntime()
{
Material material = new Material(Shader.Find("Unlit/Color"));
Color color = Color.green;
material.color = color;
float width = 0.1f;
Vector3 rightDir = boxCollider.transform.right.normalized;
Vector3 forwardDir = boxCollider.transform.forward.normalized;
Vector3 upDir = boxCollider.transform.up.normalized;
Vector3 center = boxCollider.transform.position boxCollider.center;
Vector3 size = boxCollider.size;
size.x *= boxCollider.transform.lossyScale.x;
size.y *= boxCollider.transform.lossyScale.y;
size.z *= boxCollider.transform.lossyScale.z;
DrawLine(center upDir * size.y / 2f rightDir * size.x / 2f forwardDir * size.z / 2f, center upDir * size.y / 2f - rightDir * size.x / 2f forwardDir * size.z / 2f, color, material, width);
DrawLine(center - upDir * size.y / 2f rightDir * size.x / 2f forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f rightDir * size.x / 2f forwardDir * size.z / 2f, center - upDir * size.y / 2f rightDir * size.x / 2f forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f - rightDir * size.x / 2f forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f rightDir * size.x / 2f - forwardDir * size.z / 2f, center upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center - upDir * size.y / 2f rightDir * size.x / 2f - forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f rightDir * size.x / 2f - forwardDir * size.z / 2f, center - upDir * size.y / 2f rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f rightDir * size.x / 2f forwardDir * size.z / 2f, center upDir * size.y / 2f rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center - upDir * size.y / 2f rightDir * size.x / 2f forwardDir * size.z / 2f, center - upDir * size.y / 2f rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center upDir * size.y / 2f - rightDir * size.x / 2f forwardDir * size.z / 2f, center upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
DrawLine(center - upDir * size.y / 2f - rightDir * size.x / 2f forwardDir * size.z / 2f, center - upDir * size.y / 2f - rightDir * size.x / 2f - forwardDir * size.z / 2f, color, material, width);
}
private void DrawLine(Vector3 start, Vector3 end, Color color, Material material, float width = 0.01f)
{
line = new GameObject("Line_" start.ToString() "_" end.ToString()).AddComponent<LineRenderer>();
line.name = "LineRenderer Draw Line";
line.material = material;
line.startColor = color;
line.endColor = color;
line.startWidth = width;
line.endWidth = width;
line.positionCount = 2;
line.useWorldSpace = true;
line.SetPosition(0, start);
line.SetPosition(1, end);
line.transform.SetParent(transform);
lines.Add(line);
}
public void SetLinesColor(Color color)
{
for (int i = 0; i < lines.Count; i )
{
lines[i].material.color = color;
lines[i].startColor = color;
lines[i].endColor = color;
}
}
}
uj5u.com熱心網友回復:
以下條件將SceneView和GameView分開。如果上面的代碼都沒有問題,答案就是這樣。
DrawGizmosOnRunTime(SceneView.currentDrawingSceneView() ? Color.red : Color.green);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488616.html