一、Interactable腳本
Interactable是一個Unity3D中的腳本,用於將物體標記為可交互。一個被標記為Interactable的物體可以被玩家或其他遊戲中的對象“觸摸”,並且可以觸發相應的行為。
例如,當玩家點擊標記為Interactable的按鈕時,按鈕將觸發一個事件,如彈出一個菜單、播放動畫等。
public class Interactable : MonoBehaviour { public UnityEvent OnTouch; private void OnMouseDown() { OnTouch?.Invoke(); } }
二、InteractableObject獲取觸摸物體
InteractableObject是一個基於Interactable的腳本,它允許玩家與Interactable物體進行交互。
使用InteractableObject腳本需要提前把Interactable腳本添加到需要交互的物體上。
當玩家與物體進行交互時,InteractableObject將返回玩家所觸摸的物體,這可以幫助我們進行其他行為的操作,例如可拖動對象、放置對象等交互行為。
public class InteractableObject : MonoBehaviour { private Interactable currentObject = null; private void FixedUpdate() { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { Interactable obj = hit.collider.GetComponent(); if (obj != currentObject) { if(currentObject != null) { currentObject.OnTouch.RemoveListener(this.OnSelectObject); } currentObject = obj; if(currentObject != null) { currentObject.OnTouch.AddListener(this.OnSelectObject); } } } else { if(currentObject != null) { currentObject.OnTouch.RemoveListener(this.OnSelectObject); } } } private void OnSelectObject() { Debug.Log("Selected Object: " + currentObject.gameObject.name); // Add your interactive action here } }
三、Interactable Python
Interactable Python是基於Python編寫的一個便捷的框架,用於編寫交互式應用程序。
使用Interactable Python,可以通過在Python腳本中寫入代碼來處理用戶輸入和輸出。
import interactable as ia def my_function(): ia.print("Hello, world!") ia.run(my_function)
四、Interactable
Interactable類是由C#編寫的,它提供了一組有用的方法,用於處理與Interactable對象的交互。
例如,它可以派生出一些用於特定類型對象的子類,例如InteractableButton、InteractableSlider等。
public class Interactable { public GameObject gameObject { get; set; } //Register this object with user input system public void Register() { // Add your code here } //Unregister this object with user input system public void Unregister() { // Add your code here } }
五、Interact
Interact是一個可以被添加到Interactable對象中的腳本,它提供了一組用於處理對象交互的方法。
例如,當對象被點擊或當鼠標滑過對象時,Interact腳本將觸發相應的事件,如OnHoverEnter、OnHoverExit、OnSelect等。
public class Interact : MonoBehaviour { public UnityEvent OnHoverEnter; public UnityEvent OnHoverExit; public UnityEvent OnSelect; private void OnMouseEnter() { OnHoverEnter?.Invoke(); } private void OnMouseExit() { OnHoverExit?.Invoke(); } private void OnMouseDown() { OnSelect?.Invoke(); } }
六、Interact With
Interact With是一組方法,它們可以幫助我們處理對象之間的交互。
例如,在操作一個物體時,可以用 InteractWith 函數找到另一個物體來進行操作,並且可以使用 PerformAction 函數執行相應的操作。
public class InteractWith : MonoBehaviour { public GameObject targetObject; public void PerformAction() { // Add your code here } private void OnMouseDown() { InteractWith other = targetObject.GetComponent(); if(other != null) { other.PerformAction(); } } }
七、Establish
Establish是基於Interactable的一種行為,它用於建立對其他對象的連接或通信。
例如,我們可以使用Establish函數建立與另一個Interactable的連接,並在連接上進行數據傳輸或發送消息。
public class Establish : Interactable { public void EstablishConnection(Interactable other) { // Add your code here } public void SendMessage(string message) { // Add your code here } }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246013.html