mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-14 14:29:52 -05:00
Unity 2023.1以降のバージョンでUnityEngine.Object.FindObjectsOfType系APIがObsoleteになり、次の警告が発生していました。 ``` Assets/VRM10_Samples/VRM10Viewer/VRM10ViewerUI.cs(250,24): warning CS0618: 'Object.FindObjectOfType<T>()' is obsolete: 'Object.FindObjectOfType has been deprecated. Use Object.FindFirstObjectByType instead or if finding any instance is acceptable the faster Object.FindAnyObjectByType' Assets/VRM10_Samples/VRM10Viewer/VRM10ViewerUI.cs(208,31): warning CS0618: 'Object.FindObjectsOfType<T>()' is obsolete: 'Object.FindObjectsOfType has been deprecated. Use Object.FindObjectsByType instead which lets you decide whether you need the results sorted or not. FindObjectsOfType sorts the results by InstanceID but if you do not need this using FindObjectSortMode.None is considerably faster.' ``` 代わりにFindObjectsBy系APIを使うようにしました。これはUnity 2021.3にも存在しているので、そのまま使うことができました。 FindObjectOfType()とFindFirstObjectByType()の違いに関してはドキュメントからは読み取れませんでしたが、 Unity-Technologiesgが公開しているUnity 6000のソースコードを見る限り、動作は同一に見えるためそのまま置き換えました。 https://github.com/Unity-Technologies/UnityCsReference/blob/6000.0/Runtime/Export/Scripting/UnityEngineObject.bindings.cs#L586-L602
92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
#pragma warning disable 0414, 0649
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
|
|
namespace VRM
|
|
{
|
|
/// <summary>
|
|
/// ファーストパーソン向けLayer検討
|
|
///
|
|
/// * Default LayerをFirstPersonレイヤーとして使う
|
|
/// * 9番にThirdPerson Layerを追加する
|
|
///
|
|
/// * FirstPersonCameraはCullingMaskでThirdPerson Layerを除外
|
|
/// * ThirdPersonCameraはCullingMaskでDefault Layerを除外
|
|
///
|
|
/// * それ以外のシーンオブジェクトはDefaultLayerとThirdPersonレイヤーの両方に所属するべし
|
|
/// * 首無しモデルはDefault Layerのみに所属するべし
|
|
/// * 首有りモデルはThirdPerson Layerのみに所属するべし
|
|
/// * コントローラーはDefault Layerがいいかも
|
|
/// * 鏡もDefault Layerがいいかも(カメラごとにRenderTargetを用意するのは煩雑)
|
|
/// </summary>
|
|
public class VRMFirstPersonCameraManager : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
class CameraWithRawImage
|
|
{
|
|
public Camera Camera;
|
|
public RenderTexture Texture;
|
|
public RawImage Image;
|
|
}
|
|
|
|
/// <summary>
|
|
/// FirstPerson
|
|
/// </summary>
|
|
[SerializeField]
|
|
CameraWithRawImage m_topLeft;
|
|
|
|
/// <summary>
|
|
/// ThirdPerson body
|
|
/// </summary>
|
|
[SerializeField]
|
|
CameraWithRawImage m_topRight;
|
|
|
|
/// <summary>
|
|
/// ThirdPerson head
|
|
/// </summary>
|
|
[SerializeField]
|
|
CameraWithRawImage m_bottomRight;
|
|
|
|
[SerializeField, Header("Cameras")]
|
|
Camera m_firstPersonCamera;
|
|
|
|
[SerializeField]
|
|
Camera[] m_thirdPersonCameras;
|
|
|
|
void Reset()
|
|
{
|
|
var cameras = GameObject.FindObjectsByType<Camera>(FindObjectsSortMode.InstanceID);
|
|
m_firstPersonCamera = Camera.main;
|
|
m_thirdPersonCameras = cameras.Where(x => x != m_firstPersonCamera).ToArray();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
var halfWidth = Screen.width / 2;
|
|
var halfHeight = Screen.height / 2;
|
|
SetupRenderTarget(m_topLeft, halfWidth, halfHeight);
|
|
SetupRenderTarget(m_topRight, halfWidth, halfHeight);
|
|
SetupRenderTarget(m_bottomRight, halfWidth, halfHeight);
|
|
}
|
|
|
|
void SetupRenderTarget(CameraWithRawImage cameraWithImage, int w, int h)
|
|
{
|
|
if (cameraWithImage.Camera == null) return;
|
|
if (cameraWithImage.Image == null) return;
|
|
|
|
if (cameraWithImage.Texture == null
|
|
|| cameraWithImage.Texture.width != w
|
|
|| cameraWithImage.Texture.height != h
|
|
)
|
|
{
|
|
var texture = new RenderTexture(w, h, 16);
|
|
cameraWithImage.Texture = texture;
|
|
cameraWithImage.Camera.targetTexture = texture;
|
|
cameraWithImage.Image.texture = texture;
|
|
}
|
|
}
|
|
}
|
|
}
|