Files
2026-03-04 10:03:45 +08:00

404 lines
8.9 KiB
C#

using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
namespace CTS
{
public class CTSDemoController : MonoBehaviour
{
[Header("Target")]
public GameObject m_target;
[Header("Walk Controller")]
public CTSWalk m_walkController;
private CharacterController m_characterController;
[Header("Fly Controller")]
public CTSFly m_flyController;
[Header("Look Controller")]
public CTSLook m_lookController;
[Header("Profiles")]
public CTSProfile m_unityProfile;
public CTSProfile m_liteProfile;
public CTSProfile m_basicProfile;
public CTSProfile m_advancedProfile;
public CTSProfile m_tesselatedProfile;
[Header("UX Text")]
public Text m_mode;
public Text m_readme;
public Text m_instructions;
[Header("Post FX")]
public ScriptableObject m_postFX;
private Component m_postProcessingComponent;
private void Awake()
{
if (m_target == null)
{
m_target = Camera.main.gameObject;
}
try
{
if (m_postFX != null)
{
Camera camera = Camera.main;
if (camera == null)
{
camera = UnityEngine.Object.FindObjectOfType<Camera>();
}
if (camera != null)
{
Type type = GetType("UnityEngine.PostProcessing.PostProcessingBehaviour");
if (type != null)
{
GameObject gameObject = camera.gameObject;
m_postProcessingComponent = gameObject.GetComponent(type);
if (m_postProcessingComponent == null)
{
m_postProcessingComponent = gameObject.AddComponent(type);
}
if (m_postProcessingComponent != null)
{
FieldInfo field = type.GetField("profile", BindingFlags.Instance | BindingFlags.Public);
if (field != null)
{
field.SetValue(m_postProcessingComponent, m_postFX);
}
((MonoBehaviour)m_postProcessingComponent).enabled = false;
}
}
}
}
}
catch (Exception)
{
Debug.Log("Failed to set up post fx.");
}
if (m_flyController == null)
{
m_flyController = m_target.GetComponent<CTSFly>();
}
if (m_flyController == null)
{
m_flyController = m_target.AddComponent<CTSFly>();
}
m_flyController.enabled = false;
if (m_characterController == null)
{
m_characterController = m_target.GetComponent<CharacterController>();
}
if (m_characterController == null)
{
m_characterController = m_target.AddComponent<CharacterController>();
m_characterController.height = 4f;
}
m_characterController.enabled = false;
if (m_walkController == null)
{
m_walkController = m_target.GetComponent<CTSWalk>();
}
if (m_walkController == null)
{
m_walkController = m_target.AddComponent<CTSWalk>();
m_walkController.m_controller = m_characterController;
}
m_walkController.enabled = false;
if (m_lookController == null)
{
m_lookController = m_target.GetComponent<CTSLook>();
}
if (m_lookController == null)
{
m_lookController = m_target.AddComponent<CTSLook>();
m_lookController._playerRootT = m_target.transform;
m_lookController._cameraT = m_target.transform;
}
m_lookController.enabled = false;
if (m_instructions != null)
{
string text = "";
if (m_unityProfile != null)
{
text += "Controls: 1. Unity";
}
if (m_liteProfile != null)
{
text = ((text.Length <= 0) ? "Controls: 2. Lite" : (text + ", 2. Lite"));
}
if (m_basicProfile != null)
{
text = ((text.Length <= 0) ? "Controls: 3. Basic" : (text + ", 3. Basic"));
}
if (m_advancedProfile != null)
{
text = ((text.Length <= 0) ? "Controls: 4. Advanced" : (text + ", 4. Advanced"));
}
if (m_tesselatedProfile != null)
{
text = ((text.Length <= 0) ? "Controls: 5. Tesselated" : (text + ", 5. Tesselated"));
}
if (m_flyController != null)
{
text = ((text.Length <= 0) ? "Controls: 6. Fly" : (text + ", 6. Fly"));
}
if (m_walkController != null)
{
text = ((text.Length <= 0) ? "Controls: 7. Walk" : (text + ", 7. Walk"));
}
if (m_postProcessingComponent != null)
{
text = ((text.Length <= 0) ? "Controls: P. Post FX" : (text + ", P. Post FX"));
}
text = ((text.Length <= 0) ? "Controls: ESC. Exit." : (text + ", ESC. Exit."));
m_instructions.text = text;
}
SelectBasic();
if (m_flyController != null)
{
m_flyController.enabled = false;
}
if (m_walkController != null)
{
m_walkController.enabled = false;
}
if (m_characterController != null)
{
m_characterController.enabled = false;
}
if (m_lookController != null)
{
m_lookController.enabled = false;
}
}
public void SelectUnity()
{
if (m_unityProfile != null)
{
CTSSingleton<CTSTerrainManager>.Instance.BroadcastProfileSelect(m_unityProfile);
if (m_mode != null)
{
m_mode.text = "Unity";
}
}
}
public void SelectLite()
{
if (m_liteProfile != null)
{
CTSSingleton<CTSTerrainManager>.Instance.BroadcastProfileSelect(m_liteProfile);
if (m_mode != null)
{
m_mode.text = "Lite";
}
}
}
public void SelectBasic()
{
if (m_basicProfile != null)
{
CTSSingleton<CTSTerrainManager>.Instance.BroadcastProfileSelect(m_basicProfile);
if (m_mode != null)
{
m_mode.text = "Basic";
}
}
}
public void SelectAdvanced()
{
if (m_advancedProfile != null)
{
CTSSingleton<CTSTerrainManager>.Instance.BroadcastProfileSelect(m_advancedProfile);
if (m_mode != null)
{
m_mode.text = "Advanced";
}
}
}
public void SelectTesselated()
{
if (m_tesselatedProfile != null)
{
CTSSingleton<CTSTerrainManager>.Instance.BroadcastProfileSelect(m_tesselatedProfile);
if (m_mode != null)
{
m_mode.text = "Tesselated";
}
}
}
public void Fly()
{
if (m_flyController != null && !m_flyController.isActiveAndEnabled)
{
if (m_characterController != null)
{
m_characterController.enabled = false;
}
if (m_walkController != null && m_walkController.isActiveAndEnabled)
{
m_walkController.enabled = false;
}
if (m_lookController != null)
{
m_lookController.enabled = true;
}
m_flyController.enabled = true;
}
}
public void Walk()
{
if (m_walkController != null && !m_walkController.isActiveAndEnabled)
{
if (m_flyController != null && m_flyController.isActiveAndEnabled)
{
m_flyController.enabled = false;
}
if (m_characterController != null)
{
m_characterController.enabled = true;
}
if (m_lookController != null)
{
m_lookController.enabled = true;
}
m_walkController.enabled = true;
}
}
public void PostFX()
{
if (m_postProcessingComponent != null)
{
if (((MonoBehaviour)m_postProcessingComponent).isActiveAndEnabled)
{
((MonoBehaviour)m_postProcessingComponent).enabled = false;
}
else
{
((MonoBehaviour)m_postProcessingComponent).enabled = true;
}
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
SelectUnity();
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
SelectLite();
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
SelectBasic();
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
SelectAdvanced();
}
else if (Input.GetKeyDown(KeyCode.Alpha5))
{
SelectTesselated();
}
else if (Input.GetKeyDown(KeyCode.Alpha6))
{
Fly();
}
else if (Input.GetKeyDown(KeyCode.Alpha7))
{
Walk();
}
else if (Input.GetKeyDown(KeyCode.P))
{
PostFX();
}
else if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
public static Type GetType(string TypeName)
{
Type type = Type.GetType(TypeName);
if (type != null)
{
return type;
}
if (TypeName.Contains("."))
{
string assemblyString = TypeName.Substring(0, TypeName.IndexOf('.'));
try
{
Assembly assembly = Assembly.Load(assemblyString);
if (assembly == null)
{
return null;
}
type = assembly.GetType(TypeName);
if (type != null)
{
return type;
}
}
catch (Exception)
{
}
}
Assembly callingAssembly = Assembly.GetCallingAssembly();
if (callingAssembly != null)
{
type = callingAssembly.GetType(TypeName);
if (type != null)
{
return type;
}
}
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.GetLength(0); i++)
{
type = assemblies[i].GetType(TypeName);
if (type != null)
{
return type;
}
}
AssemblyName[] referencedAssemblies = callingAssembly.GetReferencedAssemblies();
for (int j = 0; j < referencedAssemblies.Length; j++)
{
Assembly assembly2 = Assembly.Load(referencedAssemblies[j]);
if (assembly2 != null)
{
type = assembly2.GetType(TypeName);
if (type != null)
{
return type;
}
}
}
return null;
}
}
}