Udon 基础
本页面包含如何使用 Udon 的示例。所有示例都可以在 Udon Graph 或 UdonSharp 中查看。
旋转立方体
此行为每秒钟绕局部 Y 轴将一个游戏对象(如立方体)旋转 90 度。
- Udon Graph
- UdonSharp

using UnityEngine;
using VRC.SDKBase;
public class RotatingCubeBehaviour : UdonSharpBehaviour
{
private void Update()
{
transform.Rotate(Vector3.up, 90f * Time.deltaTime);
}
}
交互
此行为使用 Interact 允许玩家与对象交互以禁用它。例如,这可以用于一个消息或一扇门,当玩家点击时消失。游戏对象必须有一个碰撞器组件才能让玩家与之交互。
- Udon Graph
- UdonSharp

using UnityEngine;
using VRC.SDKBase;
public class ClickMe: UdonSharpBehaviour
{
public override void Interact()
{
gameObject.SetActive(false);
}
}
传送玩家
此行为使用 Interact 和 TeleportTo 来传送玩家。targetPositon 变换决定了玩家传送后的目标和旋转。不要忘记为 targetPosition 游戏对象添加碰撞器组件。
- Udon Graph
- UdonSharp

using UnityEngine;
using VRC.SDKBase;
public class TeleportPlayer : UdonSharpBehaviour
{
public Transform targetPosition;
public override void Interact()
{
Networking.LocalPlayer.TeleportTo(
targetPosition.position,
targetPosition.rotation);
}
}
发送事件
此行为展示了如何与其他行为交互。UdonBehaviour 可以通过变量和自定义事件相互通信。
- Udon Graph
- UdonSharp

using UdonSharp;
using UnityEngine;
using VRC.Udon.Common.Interfaces;
public class SomeExample : UdonSharpBehaviour
{
[SerializeField] private SomeOtherExample otherBehaviour;
void Start()
{
if(otherBehaviour.somePublicBoolean)
{
otherBehaviour.SomeCustomEvent();
}
}
public override void Interact()
{
DoStuff();
}
private void DoStuff()
{
SendCustomNetworkEvent(NetworkEventTarget.All, nameof(DoNetworkEventStuff));
}
public void DoNetworkEventStuff()
{
otherBehaviour.somePublicBoolean = false;
otherBehaviour.SomeCustomEvent();
otherBehaviour.SendCustomNetworkEvent(NetworkEventTarget.Owner, nameof(DoOwnerStuff));
}
}