VR 콘텐츠 기초

OculusVR - 큐브 만지면 가운데 있는 큐브 색 변경

다모아 2023. 10. 20. 12:54

큐브 색 조정

 

만지면 가운데 큐브가 그 내가 만진 큐브의 색으로 변하게 만들어줬다.

 

대리자사용 X

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube : MonoBehaviour
{
    [SerializeField]
    private Material matUnSelect;
    [SerializeField]
    private Material matSelect;
    [SerializeField]
    private TestCube cube;

    private MeshRenderer meshRenderer;
    private void Start()
    {
        this.meshRenderer = this.GetComponent<MeshRenderer>();
    }

    public void OnWhenSelect()
    {
        this.cube.meshRenderer.material = this.matSelect;
    }

    public void OnWhenUnSelect()
    {
        this.cube.meshRenderer.material = this.matUnSelect;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCube : MonoBehaviour
{
    public MeshRenderer meshRenderer;
    // Start is called before the first frame update
    void Start()
    {
        this.meshRenderer = GetComponent<MeshRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

대리자사용

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube : MonoBehaviour
{
    public enum eColor
    {
        White, Red, Blue, Green
    }

    public eColor color;
    public Action<eColor> onWhenSelect;
    public Action<eColor> onWhenUnSelect;


    public void OnWhenSelect()
    {
        this.onWhenSelect(this.color);
    }

    public void OnWhenUnSelect()
    {
        this.onWhenUnSelect(this.color);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCubeMain : MonoBehaviour
{
    [SerializeField] private Cube[] cubes;
    [SerializeField] private Material[] materials;
    [SerializeField] private MeshRenderer cube;
    // Start is called before the first frame update
    void Start()
    {
        foreach(Cube cube in cubes)
        {
            cube.onWhenSelect = (color) => this.DisPlay(color);
            cube.onWhenUnSelect = (color) => this.DisPlay(Cube.eColor.White);
        }
    }

    private void DisPlay(Cube.eColor color)
    {
        this.cube.material = this.materials[(int)color];
    }
}

Cubes는 Cube cs를 가진 Red, Blue, Green을 가져와줬고

Materials는 Material을 추가해서 손으로 집었을 때 무슨 색으로 변경하느냐로 해주었다.

마지막으로 MeshRenderer를 받아서 내가 변경시킬 MeshRenderer를 추가해주었다.