Add ultimate xr

This commit is contained in:
2024-08-06 21:58:35 +02:00
parent 864033bf10
commit 7165bacd9d
3952 changed files with 2162037 additions and 35 deletions

View File

@@ -0,0 +1,51 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrInstanceEventArgs.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using UnityEngine;
namespace UltimateXR.Core.Instantiation
{
/// <summary>
/// Instantiation event args for <see cref="UxrInstanceManager" />.
/// </summary>
public class UxrInstanceEventArgs : EventArgs
{
#region Public Types & Data
/// <summary>
/// Gets the instance of the instantiation/destroy operation. Will be null on
/// <see cref="UxrInstanceManager.Instantiating" /> and
/// <see cref="UxrInstanceManager.Destroyed" /> events, since the instance will not exist.
/// </summary>
public GameObject Instance { get; }
/// <summary>
/// Gets the prefab of the instantiation/destroy operation. Null when using
/// <see cref="UxrInstanceManager.InstantiateEmptyGameObject" />.
/// </summary>
public GameObject Prefab { get; }
/// <summary>
/// Gets the prefab Id of the instantiation/destroy operation. The prefab Id is the Id assigned by Unity to the prefab
/// asset. Null when using <see cref="UxrInstanceManager.InstantiateEmptyGameObject" /> for instantiation.
/// </summary>
public string PrefabId { get; }
#endregion
#region Constructors & Finalizer
/// <inheritdoc />
public UxrInstanceEventArgs(GameObject instance, GameObject prefab, string prefabId)
{
Instance = instance;
Prefab = prefab;
PrefabId = prefabId;
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8959f68d36624e93b7749b11fc9192f6
timeCreated: 1708255563

View File

@@ -0,0 +1,185 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrInstanceManager.InstanceInfo.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using UltimateXR.Core.Serialization;
using UltimateXR.Core.Unique;
using UltimateXR.Extensions.Unity;
using UnityEngine;
namespace UltimateXR.Core.Instantiation
{
public partial class UxrInstanceManager
{
#region Private Types & Data
/// <summary>
/// Stores instantiation information.
/// </summary>
private class InstanceInfo : IUxrSerializable
{
#region Public Types & Data
/// <summary>
/// Gets the Id of the prefab that was instantiated. Null if it's an empty GameObject.
/// </summary>
public string PrefabId => _prefabId;
/// <summary>
/// Gets the name of the empty instance, if the instance was created using
/// <see cref="UxrInstanceManager.InstantiateEmptyGameObject" />.
/// </summary>
public string Name => _name;
/// <summary>
/// Gets the parent if the object was parented to any.
/// </summary>
public IUxrUniqueId Parent => _parent;
/// <summary>
/// Gets the position. It will contain relative position to the parent if parented.
/// </summary>
public Vector3 Position => _position;
/// <summary>
/// Gets the rotation. It will contain relative rotation to the parent if parented.
/// </summary>
public Quaternion Rotation => _rotation;
/// <summary>
/// Gets the local scale.
/// </summary>
public Vector3 Scale => _scale;
#endregion
#region Constructors & Finalizer
/// <summary>
/// Constructor.
/// </summary>
/// <param name="instance">The instance</param>
/// <param name="prefabId">
/// The id of the prefab that was instantiated or null if the instance was created using
/// <see cref="UxrInstanceManager.InstantiateEmptyGameObject" />.
/// </param>
public InstanceInfo(IUxrUniqueId instance, string prefabId)
{
_prefabId = prefabId;
_name = string.IsNullOrEmpty(prefabId) ? instance.GameObject.name : null;
_parent = instance.Transform.parent != null ? instance.Transform.parent.GetComponent<IUxrUniqueId>() : null;
_position = _parent != null ? instance.Transform.localPosition : instance.Transform.position;
_rotation = _parent != null ? instance.Transform.localRotation : instance.Transform.rotation;
_scale = instance.Transform.localScale;
}
/// <summary>
/// Default constructor required for serialization.
/// </summary>
private InstanceInfo()
{
}
#endregion
#region Implicit IUxrSerializable
/// <inheritdoc />
public int SerializationVersion => 0;
/// <inheritdoc />
public void Serialize(IUxrSerializer serializer, int serializationVersion)
{
serializer.Serialize(ref _prefabId);
serializer.Serialize(ref _name);
serializer.SerializeUniqueComponent(ref _parent);
serializer.Serialize(ref _position);
serializer.Serialize(ref _rotation);
serializer.Serialize(ref _scale);
}
#endregion
#region Public Methods
/// <summary>
/// Updates the info using the current object data.
/// </summary>
/// <param name="instance">The GameObject to update the information of</param>
public void UpdateInfoUsingObject(GameObject instance)
{
Transform transform = instance.transform;
IUxrUniqueId parent = transform.parent != null ? transform.parent.GetComponent<IUxrUniqueId>() : null;
Transform parentTransform = parent?.Transform;
if (parentTransform != null)
{
// Use relative parent data
_parent = parent;
_position = transform.localPosition;
_rotation = transform.localRotation;
}
else
{
// Use world data
_parent = null;
_position = transform.position;
_rotation = transform.rotation;
}
_scale = transform.localScale;
}
/// <summary>
/// Updates the object using the current information.
/// </summary>
/// <param name="instance">The GameObject to update</param>
public void UpdateObjectUsingInfo(GameObject instance)
{
Transform transform = instance.transform;
Transform parentTransform = Parent?.Transform;
if (parentTransform != null)
{
// Use relative parent data
if (transform.parent != parentTransform)
{
transform.SetParent(parentTransform);
}
TransformExt.SetLocalPositionAndRotation(transform, Position, Rotation);
}
else
{
// Use world
if (transform.parent != null)
{
transform.SetParent(null);
}
transform.SetPositionAndRotation(Position, Rotation);
}
transform.localScale = Scale;
}
#endregion
#region Private Types & Data
private string _prefabId;
private string _name;
private IUxrUniqueId _parent;
private Vector3 _position;
private Quaternion _rotation;
private Vector3 _scale;
#endregion
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: afa00701315f483ab45afb6d9b6fa2b7
timeCreated: 1708512591

View File

@@ -0,0 +1,144 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrInstanceManager.StateSave.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using UltimateXR.Core.StateSave;
using UltimateXR.Core.Unique;
using UltimateXR.Extensions.System.Collections;
using UnityEngine;
namespace UltimateXR.Core.Instantiation
{
public partial class UxrInstanceManager
{
#region Protected Overrides UxrComponent
/// <inheritdoc />
protected override int SerializationOrder => UxrConstants.Serialization.SerializationOrderInstanceManager;
/// <inheritdoc />
protected override void SerializeState(bool isReading, int stateSerializationVersion, UxrStateSaveLevel level, UxrStateSaveOptions options)
{
base.SerializeState(isReading, stateSerializationVersion, level, options);
// Individual instantiations are already handled through events.
// We save all generated instances in higher save levels.
if (level > UxrStateSaveLevel.ChangesSincePreviousSave)
{
// When writing, update instance info first
if (!isReading)
{
foreach (KeyValuePair<Guid, InstanceInfo> pair in _currentInstancedPrefabs)
{
if (_currentInstances.TryGetValue(pair.Key, out GameObject instance) && instance != null)
{
pair.Value.UpdateInfoUsingObject(instance);
}
}
}
// We don't want to compare dictionaries, we save the instantiation info always by using null as name to avoid overhead.
SerializeStateValue(level, options, null, ref _currentInstancedPrefabs);
if (isReading)
{
// When reading we need to update the scene:
// -Destroy instances that do not exist anymore in the deserialized data
// -Create instances that are not yet in the scene, from the deserialized data
// First destroy existing instances in the scene that are not present in the deserialized list anymore:
List<Guid> toRemove = null;
foreach (KeyValuePair<Guid, GameObject> pair in _currentInstances)
{
if (pair.Value == null)
{
continue;
}
if (_currentInstancedPrefabs == null || !_currentInstancedPrefabs.ContainsKey(pair.Key))
{
// Before destroying the GameObject, unregister the components ahead of time too in case they are going to be re-created.
IUxrUniqueId[] components = pair.Value.GetComponentsInChildren<IUxrUniqueId>(true);
components.ForEach(c => c.Unregister());
Destroy(pair.Value);
if (toRemove == null)
{
toRemove = new List<Guid>();
}
toRemove.Add(pair.Key);
}
else if(_currentInstancedPrefabs != null && _currentInstancedPrefabs.TryGetValue(pair.Key, out InstanceInfo info))
{
// If the object is still present, update it to the current state
info.UpdateObjectUsingInfo(pair.Value);
}
}
if (toRemove != null)
{
foreach (Guid guid in toRemove)
{
_currentInstances.Remove(guid);
}
}
// Now instantiate prefabs that are present in the deserialized list but not in the scene:
List<(Guid CombineGuid, InstanceInfo Info)> toAdd = null;
if (_currentInstancedPrefabs != null)
{
foreach (KeyValuePair<Guid, InstanceInfo> pair in _currentInstancedPrefabs)
{
if (!_currentInstances.ContainsKey(pair.Key))
{
if (toAdd == null)
{
toAdd = new List<(Guid, InstanceInfo)>();
}
toAdd.Add((pair.Key, pair.Value));
}
}
}
if (toAdd != null)
{
foreach ((Guid CombineGuid, InstanceInfo Info) pair in toAdd)
{
Vector3 position = pair.Info.Parent != null ? pair.Info.Parent.Transform.TransformPoint(pair.Info.Position) : pair.Info.Position;
Quaternion rotation = pair.Info.Parent != null ? pair.Info.Parent.Transform.rotation * pair.Info.Rotation : pair.Info.Rotation;
Vector3 scale = pair.Info.Scale;
if (!string.IsNullOrEmpty(pair.Info.PrefabId))
{
// Prefab
GameObject newInstance = InstantiatePrefabInternal(pair.Info.PrefabId, pair.Info.Parent, position, rotation, 0, pair.CombineGuid);
newInstance.transform.localScale = scale;
CheckNetworkSpawnPostprocess(newInstance);
}
else
{
// Empty GameObject
GameObject newObject = InstantiateEmptyGameObjectInternal(pair.Info.Name, pair.Info.Parent, position, rotation, 0, pair.CombineGuid);
newObject.transform.localScale = scale;
}
}
}
}
}
}
#endregion
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5252f824b19944e38a1031be6ef5475c
timeCreated: 1708445377

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 314ed13586b47e14182028e81ed64fed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,32 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UxrPrefabList.cs" company="VRMADA">
// Copyright (c) VRMADA, All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using UnityEngine;
namespace UltimateXR.Core.Instantiation
{
/// <summary>
/// List of user-defined instantiable prefabs used by <see cref="UxrInstanceManager" />.
/// </summary>
[CreateAssetMenu(fileName = "PrefabList", menuName = "UltimateXR/Prefab List", order = 1)]
public class UxrPrefabList : ScriptableObject
{
#region Inspector Properties/Serialized Fields
[SerializeField] private List<GameObject> _prefabList;
#endregion
#region Public Types & Data
/// <summary>
/// Gets the prefab list.
/// </summary>
public IReadOnlyList<GameObject> PrefabList => _prefabList;
#endregion
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cc759d11379460f4aa1527771fc94d52
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: