Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Created September 7, 2022 17:38
Show Gist options
  • Save shivaduke28/76fc1d5e54169876018b6c6d11d16322 to your computer and use it in GitHub Desktop.
Save shivaduke28/76fc1d5e54169876018b6c6d11d16322 to your computer and use it in GitHub Desktop.
インタラクトしたらマテリアル変更して同期するやつ
using System;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
namespace TKMNY
{
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class GlobalMaterialChanger : UdonSharpBehaviour
{
[SerializeField] Renderer renderer;
[SerializeField] int materialIndex = 0;
[SerializeField] Material material;
[SerializeField] GameObject onObject;
[SerializeField] GameObject offObject;
Material originalMaterial;
Material[] materials;
[UdonSynced, FieldChangeCallback(nameof(SyncedUseMaterial))]
bool syncedUseMaterial = false;
public bool SyncedUseMaterial
{
get => syncedUseMaterial;
set
{
syncedUseMaterial = value;
materials[materialIndex] = syncedUseMaterial ? material : originalMaterial;
renderer.materials = materials;
offObject.SetActive(!syncedUseMaterial);
onObject.SetActive(syncedUseMaterial);
}
}
void Start()
{
materials = renderer.materials;
if (materialIndex < 0 || materialIndex >= materials.Length)
{
Debug.LogError("Material Indexが不正です");
gameObject.SetActive(false);
}
originalMaterial = materials[materialIndex];
renderer.materials = materials;
if (Networking.IsOwner(gameObject))
{
offObject.SetActive(true);
onObject.SetActive(false);
}
}
public override void Interact()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
SyncedUseMaterial = !SyncedUseMaterial;
RequestSerialization();
}
void OnDestroy()
{
if (originalMaterial != null)
{
Destroy(originalMaterial);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment