Unity 广告牌技术
程序员文章站
2024-03-15 08:17:47
...
广告牌技术
简单说一下什么是广告牌技术,在游戏场景中,场景物体无论何时何地都会面向我们的视角,也就是相机。
using UnityEngine;
using System.Collections;
public class NcBillboard : NcEffectBehaviour
{
// Attribute ------------------------------------------------------------------------
public bool m_bCameraLookAt;
public bool m_bFixedObjectUp;
public bool m_bFixedStand;
public enum AXIS_TYPE {AXIS_FORWARD, AXIS_BACK, AXIS_RIGHT, AXIS_LEFT, AXIS_UP, AXIS_DOWN};
public AXIS_TYPE m_FrontAxis;
public enum ROTATION {NONE, RND, ROTATE}
public ROTATION m_RatationMode;
public enum AXIS {X=0, Y, Z};
public AXIS m_RatationAxis = AXIS.Z;
public float m_fRotationValue = 180;
protected float m_fRndValue;
protected float m_fTotalRotationValue;
protected Quaternion m_qOiginal;
// Property -------------------------------------------------------------------------
#if UNITY_EDITOR
public override string CheckProperty()
{
if (1 < gameObject.GetComponents(GetType()).Length)
return "SCRIPT_WARRING_DUPLICATE";
return ""; // no error
}
#endif
// Loop Function --------------------------------------------------------------------
void Awake()
{
}
void OnEnable()
{
#if UNITY_EDITOR
if (IsCreatingEditObject() == false)
UpdateBillboard();
#else
UpdateBillboard();
#endif
}
public void UpdateBillboard()
{
m_fRndValue = Random.Range(0,360.0f);
if (enabled)
Update();
}
void Start()
{
m_qOiginal = transform.rotation;
}
void Update()
{
if (Camera.main == null)
return;
Vector3 vecUp;
// 墨皋扼 诀氦磐甫 公矫窍绊 坷璃狼 诀氦磐甫 蜡瘤茄促
if (m_bFixedObjectUp)
// vecUp = m_qOiginal * Vector3.up;
vecUp = transform.up;
else vecUp = Camera.main.transform.rotation * Vector3.up;
if (m_bCameraLookAt)
transform.LookAt(Camera.main.transform, vecUp);
else transform.LookAt(transform.position + Camera.main.transform.rotation * Vector3.back, vecUp);
switch (m_FrontAxis)
{
case AXIS_TYPE.AXIS_FORWARD: break;
case AXIS_TYPE.AXIS_BACK: transform.Rotate(transform.up, 180, Space.World); break;
case AXIS_TYPE.AXIS_RIGHT: transform.Rotate(transform.up, 270, Space.World); break;
case AXIS_TYPE.AXIS_LEFT: transform.Rotate(transform.up, 90, Space.World); break;
case AXIS_TYPE.AXIS_UP: transform.Rotate(transform.right, 90, Space.World); break;
case AXIS_TYPE.AXIS_DOWN: transform.Rotate(transform.right, 270, Space.World); break;
}
if (m_bFixedStand)
transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z));
if (m_RatationMode == ROTATION.RND)
transform.localRotation *= Quaternion.Euler((m_RatationAxis == AXIS.X ? m_fRndValue : 0), (m_RatationAxis == AXIS.Y ? m_fRndValue : 0), (m_RatationAxis == AXIS.Z ? m_fRndValue : 0));
if (m_RatationMode == ROTATION.ROTATE)
{
float fRotValue = GetEngineDeltaTime() * m_fRotationValue;
transform.Rotate((m_RatationAxis == AXIS.X ? fRotValue : 0), (m_RatationAxis == AXIS.Y ? fRotValue : 0), (m_RatationAxis == AXIS.Z ? fRotValue : 0), Space.Self);
}
}
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.IO;
[CustomEditor(typeof(NcBillboard))]
public class NcBillboardEditor : FXMakerEditor
{
// Attribute ------------------------------------------------------------------------
protected NcBillboard m_Sel;
// Property -------------------------------------------------------------------------
// Event Function -------------------------------------------------------------------
void OnEnable()
{
m_Sel = target as NcBillboard;
m_UndoManager = new FXMakerUndoManager(m_Sel, "NcBillboard");
}
void OnDisable()
{
}
public override void OnInspectorGUI()
{
AddScriptNameField(m_Sel);
m_UndoManager.CheckUndo();
// --------------------------------------------------------------
bool bClickButton = false;
EditorGUI.BeginChangeCheck();
{
// DrawDefaultInspector();
m_Sel.m_fUserTag = EditorGUILayout.FloatField(GetCommonContent("m_fUserTag"), m_Sel.m_fUserTag);
m_Sel.m_bCameraLookAt = EditorGUILayout.Toggle(GetHelpContent("m_bCameraLookAt") , m_Sel.m_bCameraLookAt);
m_Sel.m_bFixedObjectUp = EditorGUILayout.Toggle(GetHelpContent("m_bFixedObjectUp") , m_Sel.m_bFixedObjectUp);
m_Sel.m_bFixedStand = EditorGUILayout.Toggle(GetHelpContent("m_bFixedStand") , m_Sel.m_bFixedStand);
m_Sel.m_FrontAxis = (NcBillboard.AXIS_TYPE)EditorGUILayout.EnumPopup(GetHelpContent("m_FrontAxis"), m_Sel.m_FrontAxis, GUILayout.MaxWidth(Screen.width));
m_Sel.m_RatationMode = (NcBillboard.ROTATION)EditorGUILayout.EnumPopup(GetHelpContent("m_RatationMode"), m_Sel.m_RatationMode, GUILayout.MaxWidth(Screen.width));
if (m_Sel.m_RatationMode == NcBillboard.ROTATION.RND || m_Sel.m_RatationMode == NcBillboard.ROTATION.ROTATE)
m_Sel.m_RatationAxis = (NcBillboard.AXIS)EditorGUILayout.EnumPopup(GetHelpContent("m_RatationAxis"), m_Sel.m_RatationAxis, GUILayout.MaxWidth(Screen.width));
if (m_Sel.m_RatationMode == NcBillboard.ROTATION.ROTATE)
m_Sel.m_fRotationValue = EditorGUILayout.FloatField(GetHelpContent("m_fRotationValue") , m_Sel.m_fRotationValue);
}
m_UndoManager.CheckDirty();
// --------------------------------------------------------------
if ((EditorGUI.EndChangeCheck() || bClickButton) && GetFXMakerMain())
OnEditComponent();
// ---------------------------------------------------------------------
if (GUI.tooltip != "")
m_LastTooltip = GUI.tooltip;
HelpBox(m_LastTooltip);
}
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
protected GUIContent GetHelpContent(string tooltip)
{
string caption = tooltip;
string text = FXMakerTooltip.GetHsEditor_NcBillboard(tooltip);
return GetHelpContent(caption, text);
}
protected override void HelpBox(string caption)
{
string str = caption;
if (caption == "" || caption == "Script")
str = FXMakerTooltip.GetHsEditor_NcBillboard("");
base.HelpBox(str);
}
}
protected static string[,] HcFolderPopup_Common = {
{
"- Display an image and a name together."
,"- Do not divide into sub directories; show all contents in the list."
,"- Finish operations and close the Popup window." + "\n" +
"- As the chosen operation is already saved, choose Undo first if you want to cancel" + "\n" +
"- Though the window is closed, you can use Undo function by opening the window again."
,"- Cancel save."
,"- Save in the chosen category."
,"- Remove the chosen prefab."
}
};
上一篇: 阿里笔试题 广告牌问题
下一篇: Unity BillBoard广告牌