Files
TaiWan/Assets/Roaming/Scripts/Show/ShowBoxCollider.cs

243 lines
7.0 KiB
C#
Raw Normal View History

2025-10-31 15:20:38 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ShowBoxCollider : MonoBehaviour
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><><D6B1><EFBFBD><EFBFBD>ʾ<EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>
/// </summary>
public bool Test;
/// <summary>
/// <20>ȵ<EFBFBD><C8B5><EFBFBD><EFBFBD><EFBFBD>ɫ
/// </summary>
public string ColorString = "#FF000030"; // #00000055
Color clr;
/// <summary>
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>ʾ<EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>
/// </summary>
public bool showHotTip = true;
/// <summary>
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>Box<6F><78>
/// </summary>
public bool showBox = false;
void Start()
{
ColorUtility.TryParseHtmlString(ColorString, out clr);
//if(Application.isEditor)
//{
// Test = true;
//}
}
public bool showMask;
BaseShow baseShow;
private void OnMouseOver()
{
if (baseShow == null)
{
Invoke("OnMouseEnter", 0.3f);
}
}
void OnMouseEnter()
{
baseShow = GetComponent<BaseShow>();
if (baseShow != null && baseShow.CanClick)
{
showMask = true;
if (!string.IsNullOrEmpty(baseShow.info.Title) && BaseController.CanControl)
{
TipPlayerEx.Instance.ShowTip(baseShow.info);
}
}
}
void OnMouseExit()
{
showMask = false;
TipPlayerEx.Instance.HideTip();
}
void OnRenderObject()
{
if ( Test || ( BaseController.CanControl && showHotTip && showMask))
{
var colliders = gameObject.GetComponents<BoxCollider>();
if (colliders == null)
{
Destroy(this);
}
CreateLineMaterial();
lineMaterial.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(transform.localToWorldMatrix);
//for (int i = 0; i < colliders.Length; i++)
{
// var col = colliders[i];
var col = colliders[0];
var c = col.center;
var size = col.size;
float rx = size.x / 2f;
float ry = size.y / 2f;
float rz = size.z / 2f;
Vector3 p0, p1, p2, p3;
Vector3 p4, p5, p6, p7;
p0 = c + new Vector3(-rx, -ry, rz);
p1 = c + new Vector3(rx, -ry, rz);
p2 = c + new Vector3(rx, -ry, -rz);
p3 = c + new Vector3(-rx, -ry, -rz);
p4 = c + new Vector3(-rx, ry, rz);
p5 = c + new Vector3(rx, ry, rz);
p6 = c + new Vector3(rx, ry, -rz);
p7 = c + new Vector3(-rx, ry, -rz);
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex(p0);
GL.Vertex(p1);
GL.Vertex(p2);
GL.Vertex(p3);
GL.End();
if (showBox)
{
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex(p4);
GL.Vertex(p5);
GL.Vertex(p6);
GL.Vertex(p7);
GL.End();
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex(p0);
GL.Vertex(p1);
GL.Vertex(p5);
GL.Vertex(p4);
GL.End();
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex(p1);
GL.Vertex(p2);
GL.Vertex(p6);
GL.Vertex(p5);
GL.End();
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex(p2);
GL.Vertex(p3);
GL.Vertex(p7);
GL.Vertex(p6);
GL.End();
GL.Begin(GL.QUADS);
GL.Color(clr);
GL.Vertex(p0);
GL.Vertex(p3);
GL.Vertex(p7);
GL.Vertex(p4);
GL.End();
}
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p0);
//GL.Vertex(p1);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p1);
//GL.Vertex(p2);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p2);
//GL.Vertex(p3);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p0);
//GL.Vertex(p3);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p4);
//GL.Vertex(p5);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p5);
//GL.Vertex(p6);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p6);
//GL.Vertex(p7);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p4);
//GL.Vertex(p7);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p0);
//GL.Vertex(p4);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p1);
//GL.Vertex(p5);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p2);
//GL.Vertex(p6);
//GL.End();
//GL.Begin(GL.LINES);
//GL.Color(Color.red);
//GL.Vertex(p3);
//GL.Vertex(p7);
//GL.End();
#endregion
}
GL.PopMatrix();
}
}
static Material lineMaterial;
static void CreateLineMaterial()
{
if (!lineMaterial)
{
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
}