・ItemHandFoodController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;
using UniRx;
using HoloMonApp.ConditionSpace;
namespace HoloMonApp.ItemSpace
{
public class ItemHandFoodController : MonoBehaviour
{
[Serializable]
private class ChildObjectData
{
public GameObject Object;
public Vector3 DefaultLocalPosition;
public Quaternion DefaultLocalRotation;
}
<summary>
</summary>
[SerializeField, Tooltip("コントロール対象")]
private GameObject p_ControlObject;
<summary>
</summary>
[SerializeField, Tooltip("デフォルト座標")]
private Vector3 p_DefaultWorldPosition;
<summary>
</summary>
[SerializeField, Tooltip("デフォルトサイズ")]
private Vector3 p_DefaultLocalScale;
<summary>
</summary>
[SerializeField, Tooltip("現在のスケール変化比率")]
private float p_ScaleRatio;
<summary>
</summary>
[SerializeField, Tooltip("管理子オブジェクトリスト")]
private List<ChildObjectData> p_ChildObjectDataList;
<summary>
</summary>
private IDisposable p_LerpTrigger;
<summary>
</summary>
void Start()
{
p_DefaultWorldPosition = p_ControlObject.transform.position;
p_DefaultLocalScale = p_ControlObject.transform.localScale;
SaveChildObjectData();
HoloMonConditionBodySingleton.Instance.IReadOnlyReactivePropertyHoloMonBodyStatus
.ObserveOnMainThread()
.Subscribe(status => {
float currentScaleRatio = HoloMonConditionBodySingleton.Instance.GetHeightRatio();
if (p_ScaleRatio != currentScaleRatio)
{
ApplySizeCondition(currentScaleRatio);
}
})
.AddTo(this);
}
<summary>
</summary>
void Update()
{
}
<summary>
</summary>
public void SpawnSwitch(Transform a_SpawnTransform)
{
if (p_ControlObject.activeSelf)
{
DespawnObject(a_SpawnTransform);
}
else
{
SpawnObject(a_SpawnTransform);
}
}
<summary>
</summary>
public void SpawnObject(Transform a_SpawnTransform)
{
ApplySizeCondition(HoloMonConditionBodySingleton.Instance.GetHeightRatio());
ApplyChildObjectData();
p_ControlObject.SetActive(true);
p_ControlObject.transform.position = a_SpawnTransform.position;
p_ControlObject.transform.eulerAngles = new Vector3(0.0f, a_SpawnTransform.eulerAngles.y, 0.0f);
ActiveChildObjectData();
}
<summary>
</summary>
public void DespawnObject(Transform a_SpawnTransform)
{
ApplyChildObjectData();
p_ControlObject.transform.position = a_SpawnTransform.position;
p_ControlObject.SetActive(false);
}
<summary>
</summary>
private void SaveChildObjectData()
{
foreach (ChildObjectData objectData in p_ChildObjectDataList)
{
objectData.DefaultLocalPosition = objectData.Object.transform.localPosition;
objectData.DefaultLocalRotation = objectData.Object.transform.localRotation;
}
}
<summary>
</summary>
private void ApplyChildObjectData()
{
foreach (ChildObjectData objectData in p_ChildObjectDataList)
{
Rigidbody rigid = objectData.Object.GetComponent<Rigidbody>();
if (rigid != null) rigid.velocity = Vector3.zero;
objectData.Object.transform.localPosition = objectData.DefaultLocalPosition;
objectData.Object.transform.localRotation = objectData.DefaultLocalRotation;
}
}
<summary>
</summary>
private void ActiveChildObjectData()
{
foreach (ChildObjectData objectData in p_ChildObjectDataList)
{
if (!objectData.Object.activeSelf) objectData.Object.SetActive(true);
}
}
<summary>
</summary>
<param name="a_ScaleRatio"></param>
private void ApplySizeCondition(float a_ScaleRatio)
{
float beforeRatio = p_ScaleRatio;
float afterRatio = a_ScaleRatio;
int lerpTime = 10;
p_LerpTrigger?.Dispose();
p_LerpTrigger = Observable
.Interval(TimeSpan.FromSeconds(0.05f))
.Take(lerpTime)
.SubscribeOnMainThread()
.Subscribe(
x =>
{
float current = (float)(1.0 / lerpTime) * x;
float settingRatio = Mathf.Lerp(beforeRatio, afterRatio, current);
p_ControlObject.transform.localScale = p_DefaultLocalScale * settingRatio;
},
() =>
{
p_ControlObject.transform.localScale = p_DefaultLocalScale * afterRatio;
p_ScaleRatio = a_ScaleRatio;
})
.AddTo(this);
}
}
}