diff --git a/Assets/UniGLTF/Editor/Animation/AnimationExporter.cs b/Assets/UniGLTF/Editor/Animation/AnimationExporter.cs index 9bfcdb6ce..d0f73512d 100644 --- a/Assets/UniGLTF/Editor/Animation/AnimationExporter.cs +++ b/Assets/UniGLTF/Editor/Animation/AnimationExporter.cs @@ -147,7 +147,7 @@ namespace UniGLTF var elementCount = 0; if (property == glTFAnimationTarget.AnimationProperties.BlendShape) { - var mesh = nodes[nodeIndex].GetComponent().sharedMesh; + var mesh = nodes[nodeIndex].GetComponentOrThrow().sharedMesh; elementCount = mesh.blendShapeCount; } else @@ -168,7 +168,7 @@ namespace UniGLTF float valueFactor = 1.0f; if (property == glTFAnimationTarget.AnimationProperties.BlendShape) { - var mesh = nodes[nodeIndex].GetComponent().sharedMesh; + var mesh = nodes[nodeIndex].GetComponentOrThrow().sharedMesh; var blendShapeName = binding.propertyName.Replace("blendShape.", ""); elementOffset = mesh.GetBlendShapeIndex(blendShapeName); valueFactor = 0.01f; diff --git a/Assets/UniGLTF/Editor/Animation/EditorAnimationExporter.cs b/Assets/UniGLTF/Editor/Animation/EditorAnimationExporter.cs index f7c248c99..76a4b9d18 100644 --- a/Assets/UniGLTF/Editor/Animation/EditorAnimationExporter.cs +++ b/Assets/UniGLTF/Editor/Animation/EditorAnimationExporter.cs @@ -14,14 +14,12 @@ namespace UniGLTF { var clips = new List(); - var animator = Copy.GetComponent(); - if (animator != null) + if (Copy.TryGetComponent(out var animator)) { clips.AddRange(AnimationExporter.GetAnimationClips(animator)); } - var animation = Copy.GetComponent(); - if (animation != null) + if (Copy.TryGetComponent(out var animation)) { clips.AddRange(AnimationExporter.GetAnimationClips(animation)); } diff --git a/Assets/UniGLTF/Editor/MeshUtility/TabMeshSeparator.cs b/Assets/UniGLTF/Editor/MeshUtility/TabMeshSeparator.cs index 84cdea27a..47f473d8b 100644 --- a/Assets/UniGLTF/Editor/MeshUtility/TabMeshSeparator.cs +++ b/Assets/UniGLTF/Editor/MeshUtility/TabMeshSeparator.cs @@ -190,7 +190,7 @@ namespace UniGLTF.MeshUtility var targetObjectForMeshWithoutBS = GameObject.Instantiate(srcGameObject); targetObjectForMeshWithoutBS.name = srcGameObject.name + "_WithoutBlendShape"; targetObjectForMeshWithoutBS.transform.SetParent(srcTransform); - var skinnedMeshRendererWithoutBS = targetObjectForMeshWithoutBS.GetComponent(); + var skinnedMeshRendererWithoutBS = targetObjectForMeshWithoutBS.GetComponentOrNull(); // build meshes with/without BlendShape var with = BuildNewMesh(skinnedMeshRendererInput, vertexIndexWithBlendShape, submeshesWithBlendShape, BlendShapeLogic.WithBlendShape); diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/InternalTPose.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/InternalTPose.cs index 3536ef1a1..c51f771f4 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/InternalTPose.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/InternalTPose.cs @@ -149,7 +149,7 @@ namespace UniGLTF var modelBones = GetModelBones.Invoke(null, new object[] { go.transform, false, null }); var existingMappings = new Dictionary(); - var animator = go.GetComponent(); + var animator = go.GetComponentOrThrow(); foreach (var bone in CachedEnum.GetValues()) { if (bone == HumanBodyBones.LastBone) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs index fba3887a2..bbbd3f398 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/MeshExportValidator.cs @@ -18,8 +18,7 @@ namespace UniGLTF } if (r is MeshRenderer) { - MeshFilter f = r.GetComponent(); - if (f != null) + if (r.TryGetComponent(out var f)) { return f.sharedMesh; } diff --git a/Assets/UniGLTF/Editor/UniGLTF/Gizmo/BoneInfoDrawer.cs b/Assets/UniGLTF/Editor/UniGLTF/Gizmo/BoneInfoDrawer.cs index 90d90ce7e..f16cd85b6 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/Gizmo/BoneInfoDrawer.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/Gizmo/BoneInfoDrawer.cs @@ -29,7 +29,10 @@ namespace UniGLTF { if (_boneMesh == null) { - _boneMesh = BonePrefab.GetComponent().sharedMesh; + if (BonePrefab.TryGetComponent(out var f)) + { + _boneMesh = f.sharedMesh; + } } return _boneMesh; } @@ -42,8 +45,11 @@ namespace UniGLTF { if (_boneMaterial == null) { - _boneMaterial = BonePrefab.GetComponent().sharedMaterial; - _boneMaterial.enableInstancing = true; + if (BonePrefab.TryGetComponent(out var mr)) + { + _boneMaterial = mr.sharedMaterial; + _boneMaterial.enableInstancing = true; + }; } return _boneMaterial; } @@ -76,7 +82,10 @@ namespace UniGLTF { if (_selectedBoneMesh == null) { - _selectedBoneMesh = SelectedPrefab.GetComponent().sharedMesh; + if (SelectedPrefab.TryGetComponent(out var mf)) + { + _selectedBoneMesh = mf.sharedMesh; + }; } return _selectedBoneMesh; } @@ -89,8 +98,11 @@ namespace UniGLTF { if (_selectedMaterial == null) { - _selectedMaterial = SelectedPrefab.GetComponent().sharedMaterial; - _selectedMaterial.enableInstancing = true; + if (SelectedPrefab.TryGetComponent(out var mr)) + { + _selectedMaterial = mr.sharedMaterial; + _selectedMaterial.enableInstancing = true; + } } return _selectedMaterial; } @@ -123,7 +135,10 @@ namespace UniGLTF { if (_hoverBoneMesh == null) { - _hoverBoneMesh = HoverPrefab.GetComponent().sharedMesh; + if (HoverPrefab.TryGetComponent(out var mf)) + { + _hoverBoneMesh = mf.sharedMesh; + } } return _hoverBoneMesh; } @@ -136,8 +151,11 @@ namespace UniGLTF { if (_hoverMaterial == null) { - _hoverMaterial = HoverPrefab.GetComponent().sharedMaterial; - _hoverMaterial.enableInstancing = true; + if (HoverPrefab.TryGetComponent(out var mr)) + { + _hoverMaterial = mr.sharedMaterial; + _hoverMaterial.enableInstancing = true; + } } return _hoverMaterial; } diff --git a/Assets/UniGLTF/Editor/UniHumanoid/BoneMappingEditor.cs b/Assets/UniGLTF/Editor/UniHumanoid/BoneMappingEditor.cs index bdd6f444d..29ed3e1bc 100644 --- a/Assets/UniGLTF/Editor/UniHumanoid/BoneMappingEditor.cs +++ b/Assets/UniGLTF/Editor/UniHumanoid/BoneMappingEditor.cs @@ -17,8 +17,7 @@ namespace UniHumanoid { m_target = (BoneMapping)target; - var animator = m_target.GetComponent(); - if (animator != null) + if (m_target.TryGetComponent(out var animator)) { m_bones = EachBoneDefs.Select(x => new Bone( animator.GetBoneTransform(x.Head), animator.GetBoneTransform(x.Tail))) diff --git a/Assets/UniGLTF/Editor/UniHumanoid/HumanoidEditor.cs b/Assets/UniGLTF/Editor/UniHumanoid/HumanoidEditor.cs index c0a2df396..f6c95afc7 100644 --- a/Assets/UniGLTF/Editor/UniHumanoid/HumanoidEditor.cs +++ b/Assets/UniGLTF/Editor/UniHumanoid/HumanoidEditor.cs @@ -399,8 +399,7 @@ namespace UniHumanoid AssetDatabase.ImportAsset(unityPath); // replace - var animator = m_target.GetComponent(); - if (animator == null) + if (m_target.TryGetComponent(out var animator)) { animator = m_target.gameObject.AddComponent(); } diff --git a/Assets/UniGLTF/Editor/UniHumanoid/MuscleInspectorEditor.cs b/Assets/UniGLTF/Editor/UniHumanoid/MuscleInspectorEditor.cs index 609886c91..d0fb62f2b 100644 --- a/Assets/UniGLTF/Editor/UniHumanoid/MuscleInspectorEditor.cs +++ b/Assets/UniGLTF/Editor/UniHumanoid/MuscleInspectorEditor.cs @@ -312,8 +312,7 @@ namespace UniHumanoid void OnEnable() { var mi = this.target as MuscleInspector; - var animator = mi.GetComponent(); - if (animator != null + if (mi.TryGetComponent(out var animator) && animator.avatar != null && animator.avatar.isValid && animator.avatar.isHuman diff --git a/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs index 17bbb3bba..dc394f2f0 100644 --- a/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/UnityExtensions.cs @@ -399,14 +399,12 @@ namespace UniGLTF public static Mesh GetSharedMesh(this Transform t) { - var meshFilter = t.GetComponent(); - if (meshFilter != null) + if (t.TryGetComponent(out var meshFilter)) { return meshFilter.sharedMesh; } - var skinnedMeshRenderer = t.GetComponent(); - if (skinnedMeshRenderer != null) + if (t.TryGetComponent(out var skinnedMeshRenderer)) { return skinnedMeshRenderer.sharedMesh; } @@ -414,32 +412,74 @@ namespace UniGLTF return null; } - public static Material[] GetSharedMaterials(this Transform t) - { - var renderer = t.GetComponent(); - if (renderer != null) - { - return renderer.sharedMaterials; - } - - return new Material[] { }; - } - public static bool Has(this Transform transform, T t) where T : Component { - return transform.GetComponent() == t; + if (transform.TryGetComponent(out var c)) + { + return c == t; + } + return false; ; } public static T GetOrAddComponent(this GameObject go) where T : Component { - var c = go.GetComponent(); - if (c != null) + if (go.TryGetComponent(out var t)) { - return c; + return t; } return go.AddComponent(); } + public static T GetComponentOrThrow(this GameObject go) where T : Component + { + if (go.TryGetComponent(out var t)) + { + return t; + } + else + { + throw new ArgumentException($"no {nameof(T)}"); + } + } + + public static T GetComponentOrThrow(this Component c) where T : Component + { + if (c.TryGetComponent(out var t)) + { + return t; + } + else + { + throw new ArgumentException($"no {nameof(T)}"); + } + } + + public static T GetComponentOrNull(this GameObject go) where T : Component + { + if (go.TryGetComponent(out var t)) + { + return t; + } + else + { + // きれいな null を返す + return null; + } + } + + public static T GetComponentOrNull(this Component c) where T : Component + { + if (c.TryGetComponent(out var t)) + { + return t; + } + else + { + // きれいな null を返す + return null; + } + } + public static bool EnableForExport(this Component mono) { if (mono.transform.Ancestors().Any(x => !x.gameObject.activeSelf)) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/BindposeGizmo.cs b/Assets/UniGLTF/Runtime/MeshUtility/BindposeGizmo.cs index 886e7daf1..402b0fb9f 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/BindposeGizmo.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/BindposeGizmo.cs @@ -29,9 +29,10 @@ namespace UniGLTF.MeshUtility private void Reset() { - var renderer = GetComponent(); - if (renderer == null) return; - m_target = renderer.sharedMesh; + if (TryGetComponent(out var renderer)) + { + m_target = renderer.sharedMesh; + } } #if UNITY_EDITOR @@ -39,48 +40,48 @@ namespace UniGLTF.MeshUtility [ContextMenu("ToBindpose")] void ToBindpose() { - var renderer = GetComponent(); - - var root = - renderer.bones - .Select(x => Ancestors(x).Reverse().ToArray()) - .Aggregate((a, b) => - { - int i = 0; - for (; i < a.Length && i < b.Length; ++i) + if (TryGetComponent(out var renderer)) + { + var root = renderer.bones + .Select(x => Ancestors(x).Reverse().ToArray()) + .Aggregate((a, b) => { - if (a[i] != b[i]) + int i = 0; + for (; i < a.Length && i < b.Length; ++i) { - break; + if (a[i] != b[i]) + { + break; + } } - } - return a.Take(i).ToArray(); - }) - .Last() - ; + return a.Take(i).ToArray(); + }) + .Last() + ; - var map = new Dictionary(); - for (int i = 0; i < renderer.bones.Length; ++i) - { - map[renderer.bones[i]] = m_target.bindposes[i]; - } - - { - var bones = Traverse(root); - Undo.RecordObjects(bones.ToArray(), "toBindpose"); - - foreach (var x in bones) + var map = new Dictionary(); + for (int i = 0; i < renderer.bones.Length; ++i) { - var bind = default(Matrix4x4); - if (map.TryGetValue(x, out bind)) - { - var toWorld = renderer.transform.localToWorldMatrix * bind.inverse; - x.position = toWorld.GetColumn(3); - x.rotation = toWorld.ExtractRotation(); - } + map[renderer.bones[i]] = m_target.bindposes[i]; } - //EditorUtility.SetDirty(transform); + { + var bones = Traverse(root); + Undo.RecordObjects(bones.ToArray(), "toBindpose"); + + foreach (var x in bones) + { + var bind = default(Matrix4x4); + if (map.TryGetValue(x, out bind)) + { + var toWorld = renderer.transform.localToWorldMatrix * bind.inverse; + x.position = toWorld.GetColumn(3); + x.rotation = toWorld.ExtractRotation(); + } + } + + //EditorUtility.SetDirty(transform); + } } } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs b/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs index 191d3f522..39a4e96e4 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs @@ -12,22 +12,23 @@ namespace UniGLTF.MeshUtility private static MeshAttachInfo CreateMeshInfo(Transform src) { // SkinnedMeshRenderer - var smr = src.GetComponent(); - var mesh = MeshFreezer.NormalizeSkinnedMesh(smr); - if (mesh != null) + if (src.TryGetComponent(out var smr)) { - return new MeshAttachInfo + var mesh = MeshFreezer.NormalizeSkinnedMesh(smr); + if (mesh != null) { - Mesh = mesh, - Materials = smr.sharedMaterials, - Bones = smr.bones, - RootBone = smr.rootBone, - }; + return new MeshAttachInfo + { + Mesh = mesh, + Materials = smr.sharedMaterials, + Bones = smr.bones, + RootBone = smr.rootBone, + }; + } } // MeshRenderer - var mr = src.GetComponent(); - if (mr != null) + if (src.TryGetComponent(out var mr)) { var dstMesh = MeshFreezer.NormalizeNoneSkinnedMesh(mr, true); if (dstMesh != null) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs index 32b5d30cc..71c1eb368 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs @@ -194,12 +194,18 @@ namespace UniGLTF.MeshUtility { if (Application.isPlaying) { - GameObject.Destroy(r.gameObject.GetComponent()); + if (r.gameObject.TryGetComponent(out var mf)) + { + GameObject.Destroy(mf); + } GameObject.Destroy(r); } else { - GameObject.DestroyImmediate(r.gameObject.GetComponent()); + if (r.gameObject.TryGetComponent(out var mf)) + { + GameObject.DestroyImmediate(mf); + } GameObject.DestroyImmediate(r); } } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs index b0020bbd8..babd97af1 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs @@ -34,7 +34,7 @@ namespace UniGLTF.MeshUtility } ).ToArray(); - if (dst.GetComponent() is SkinnedMeshRenderer dstRenderer) + if (dst.TryGetComponent(out var dstRenderer)) { dstRenderer.sharedMesh = Mesh; dstRenderer.sharedMaterials = Materials; @@ -48,10 +48,10 @@ namespace UniGLTF.MeshUtility } else { - if (dst.GetComponent() is MeshFilter dstFilter) + if (dst.TryGetComponent(out var dstFilter)) { dstFilter.sharedMesh = Mesh; - if (dst.gameObject.GetComponent() is MeshRenderer dstRenderer) + if (dst.gameObject.TryGetComponent(out var dstRenderer)) { dstRenderer.sharedMaterials = Materials; } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs index f11b0f2e2..7a8160bcf 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs @@ -39,8 +39,7 @@ namespace UniGLTF.MeshUtility { return true; } - var filter = mr.GetComponent(); - if (filter == null) + if (mr.TryGetComponent(out var filter)) { return true; } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs index 43fa28dad..90029be67 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs @@ -15,7 +15,7 @@ namespace UniGLTF.MeshUtility } if (r is MeshRenderer mr) { - if (mr.GetComponent() is MeshFilter mf) + if (mr.TryGetComponent(out var mf)) { return mf.sharedMesh; } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs index 30b1ecc74..56b1a01fe 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs @@ -301,10 +301,14 @@ namespace UniGLTF.MeshUtility return default; } - var srcFilter = srcRenderer.GetComponent(); - if (srcFilter == null - || srcFilter.sharedMesh == null - || srcFilter.sharedMesh.vertexCount == 0) + if (srcRenderer.TryGetComponent(out var srcFilter)) + { + if (srcFilter.sharedMesh == null || srcFilter.sharedMesh.vertexCount == 0) + { + return default; + } + } + else { return default; } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationGroup.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationGroup.cs index 301e1aa72..25c23013e 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationGroup.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationGroup.cs @@ -29,11 +29,11 @@ namespace UniGLTF.MeshUtility var relative = r.transform.RelativePathFrom(go.transform); if (r is SkinnedMeshRenderer smr) { - copy.Renderers.Add(instance.transform.GetFromPath(relative).GetComponent()); + copy.Renderers.Add(instance.transform.GetFromPath(relative).GetComponentOrNull()); } else if (r is MeshRenderer mr) { - copy.Renderers.Add(instance.transform.GetFromPath(relative).GetComponent()); + copy.Renderers.Add(instance.transform.GetFromPath(relative).GetComponentOrNull()); } } return copy; diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index dd2f29a47..d71e2b64f 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -95,12 +95,7 @@ namespace UniGLTF.MeshUtility void Push(MeshRenderer renderer) { - var meshFilter = renderer.GetComponent(); - if (meshFilter == null) - { - Debug.LogWarningFormat("{0} has no mesh filter", renderer.name); - return; - } + var meshFilter = renderer.GetComponentOrThrow(); var mesh = meshFilter.sharedMesh; if (mesh == null) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs index f918be318..02611a8d5 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExportInfo.cs @@ -38,8 +38,7 @@ namespace UniGLTF } else if (r is MeshRenderer mr) { - var filter = r.GetComponent(); - if (filter == null) + if (r.TryGetComponent(out var filter)) { return false; } @@ -201,8 +200,7 @@ namespace UniGLTF } else if (renderer is MeshRenderer mr) { - var filter = mr.GetComponent(); - if (filter != null && settings.MeshFilterAllowedHideFlags.HasFlag(filter.hideFlags)) + if (mr.TryGetComponent(out var filter) && settings.MeshFilterAllowedHideFlags.HasFlag(filter.hideFlags)) { if (filter.sharedMesh != null && filter.sharedMesh.vertexCount > 0) { @@ -411,23 +409,24 @@ namespace UniGLTF m_list.Clear(); foreach (var node in nodes) { - var renderer = node.GetComponent(); - if (renderer == null || !renderer.enabled) + if (node.TryGetComponent(out var renderer)) { - continue; - } + if (!renderer.enabled) + { + continue; + } + var found = m_list.FirstOrDefault(x => x.IsSameMeshAndMaterials(renderer)); + if (found != null) + { + found.PushRenderer(renderer); + continue; + } - var found = m_list.FirstOrDefault(x => x.IsSameMeshAndMaterials(renderer)); - if (found != null) - { - found.PushRenderer(renderer); - continue; - } - - var info = new MeshExportInfo(renderer, settings); - if (info.Mesh != null) - { - m_list.Add(info); + var info = new MeshExportInfo(renderer, settings); + if (info.Mesh != null) + { + m_list.Add(info); + } } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs index b716c0946..d18e396ba 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs @@ -173,8 +173,7 @@ namespace UniGLTF public static void SetupSkinning(GltfData data, List nodes, int i, IAxisInverter inverter) { var x = nodes[i]; - var skinnedMeshRenderer = x.Transform.GetComponent(); - if (skinnedMeshRenderer != null) + if (x.Transform.TryGetComponent(out var skinnedMeshRenderer)) { var mesh = skinnedMeshRenderer.sharedMesh; if (x.SkinIndex.HasValue) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index c3778bcfa..15d8cd9af 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -109,7 +109,7 @@ namespace UniGLTF Copy = m_tmpParent; } - if (Copy.transform.GetComponent() != null) + if (Copy.transform.TryGetComponent(out var r)) { // should throw ? Debug.LogError("root mesh is not exported"); @@ -161,12 +161,9 @@ namespace UniGLTF if (x.gameObject.activeInHierarchy) { - var meshRenderer = x.GetComponent(); - - if (meshRenderer != null && meshRenderer.enabled) + if (x.TryGetComponent(out var meshRenderer) && meshRenderer.enabled) { - var meshFilter = x.GetComponent(); - if (meshFilter != null) + if (x.TryGetComponent(out var meshFilter)) { var mesh = meshFilter.sharedMesh; var materials = meshRenderer.sharedMaterials; @@ -192,8 +189,7 @@ namespace UniGLTF } } - var skinnedMeshRenderer = x.GetComponent(); - if (skinnedMeshRenderer != null && skinnedMeshRenderer.enabled) + if (x.TryGetComponent(out var skinnedMeshRenderer) && skinnedMeshRenderer.enabled) { var mesh = skinnedMeshRenderer.sharedMesh; var materials = skinnedMeshRenderer.sharedMaterials; diff --git a/Assets/UniGLTF/Runtime/UniHumanoid/AvatarDescription.cs b/Assets/UniGLTF/Runtime/UniHumanoid/AvatarDescription.cs index 5a7f24a5b..526cffc7a 100644 --- a/Assets/UniGLTF/Runtime/UniHumanoid/AvatarDescription.cs +++ b/Assets/UniGLTF/Runtime/UniHumanoid/AvatarDescription.cs @@ -137,8 +137,7 @@ namespace UniHumanoid var avatar = CreateAvatar(root); avatar.name = name; - var animator = root.GetComponent(); - if (animator != null) + if (root.TryGetComponent(out var animator)) { var positionMap = root.Traverse().ToDictionary(x => x, x => x.position); animator.avatar = avatar; @@ -148,8 +147,7 @@ namespace UniHumanoid } } - var transfer = root.GetComponent(); - if (transfer != null) + if (root.TryGetComponent(out var transfer)) { transfer.Avatar = avatar; } diff --git a/Assets/UniGLTF/Runtime/UniHumanoid/BoneMapping.cs b/Assets/UniGLTF/Runtime/UniHumanoid/BoneMapping.cs index a3c9446ea..0510cd840 100644 --- a/Assets/UniGLTF/Runtime/UniHumanoid/BoneMapping.cs +++ b/Assets/UniGLTF/Runtime/UniHumanoid/BoneMapping.cs @@ -21,8 +21,7 @@ namespace UniHumanoid { Bones = new GameObject[(int)HumanBodyBones.LastBone]; - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { if (animator.avatar != null) { @@ -94,7 +93,7 @@ namespace UniHumanoid private void Awake() { if (Bones == null - || Bones.All(x => x==null)) + || Bones.All(x => x == null)) { GetBones(); } diff --git a/Assets/UniGLTF/Runtime/UniHumanoid/HumanPoseTransfer.cs b/Assets/UniGLTF/Runtime/UniHumanoid/HumanPoseTransfer.cs index 8597a27b5..d9ee1a829 100644 --- a/Assets/UniGLTF/Runtime/UniHumanoid/HumanPoseTransfer.cs +++ b/Assets/UniGLTF/Runtime/UniHumanoid/HumanPoseTransfer.cs @@ -45,8 +45,7 @@ namespace UniHumanoid private void Reset() { - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { Avatar = animator.avatar; } @@ -68,8 +67,7 @@ namespace UniHumanoid HumanPoseHandler m_handler; public void OnEnable() { - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { Avatar = animator.avatar; } diff --git a/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs b/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs index 068492f5d..5680e7d45 100644 --- a/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs +++ b/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs @@ -409,37 +409,38 @@ namespace UniHumanoid /// public bool AssignBonesFromAnimator() { - var animator = GetComponent(); - if (animator == null) + if (TryGetComponent(out var animator)) { - return false; - } - var avatar = animator.avatar; - if (avatar == null) - { - return false; - } - if (!avatar.isValid) - { - return false; - } - if (!avatar.isHuman) - { - return false; - } - - var keys = CachedEnum.GetValues(); - - AssignBones(keys.Select(x => - { - if (x == HumanBodyBones.LastBone) + var avatar = animator.avatar; + if (avatar == null) { - return (HumanBodyBones.LastBone, null); + return false; + } + if (!avatar.isValid) + { + return false; + } + if (!avatar.isHuman) + { + return false; } - return ((HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), x.ToString(), true), animator.GetBoneTransform(x)); - })); - return true; + var keys = CachedEnum.GetValues(); + + AssignBones(keys.Select(x => + { + if (x == HumanBodyBones.LastBone) + { + return (HumanBodyBones.LastBone, null); + } + return ((HumanBodyBones)Enum.Parse(typeof(HumanBodyBones), x.ToString(), true), animator.GetBoneTransform(x)); + })); + return true; + } + else + { + return false; + } } public bool TryGetBoneForTransform(Transform t, out HumanBodyBones bone) diff --git a/Assets/UniGLTF/Runtime/UniHumanoid/MuscleDebug.cs b/Assets/UniGLTF/Runtime/UniHumanoid/MuscleDebug.cs index b03b9dd25..38abbdcac 100644 --- a/Assets/UniGLTF/Runtime/UniHumanoid/MuscleDebug.cs +++ b/Assets/UniGLTF/Runtime/UniHumanoid/MuscleDebug.cs @@ -12,14 +12,12 @@ namespace UniHumanoid { Avatar GetAvatar() { - var animator = GetComponent(); - if (animator != null && animator.avatar != null) + if (TryGetComponent(out var animator) && animator.avatar != null) { return animator.avatar; } - var transfer = GetComponent(); - if (transfer != null && transfer.Avatar != null) + if (TryGetComponent(out var transfer) && transfer.Avatar != null) { return transfer.Avatar; } diff --git a/Assets/UniGLTF/Samples~/UniHumanoid/HumanBuilderTest.cs b/Assets/UniGLTF/Samples~/UniHumanoid/HumanBuilderTest.cs index 6c81c802d..b517b14dc 100644 --- a/Assets/UniGLTF/Samples~/UniHumanoid/HumanBuilderTest.cs +++ b/Assets/UniGLTF/Samples~/UniHumanoid/HumanBuilderTest.cs @@ -119,7 +119,7 @@ namespace UniHumanoid builder.AddLeg(0.1f, 0.3f, 0.4f, 0.1f, 0.1f); var description = AvatarDescription.Create(builder.Skeleton); - var animator = GetComponent(); + var animator = GetComponentOrThrow(); animator.avatar = description.CreateAvatar(root); // create SkinnedMesh for bone visualize @@ -132,8 +132,7 @@ namespace UniHumanoid renderer.sharedMaterial = m_material; //root.gameObject.AddComponent(); - var transfer = GetComponent(); - if (transfer != null) + if (TryGetComponent(out var transfer)) { transfer.Avatar = animator.avatar; transfer.Setup(); diff --git a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs index 67f824ff7..3766f9d4f 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs @@ -115,7 +115,7 @@ namespace UniGLTF var root = GameObject.CreatePrimitive(PrimitiveType.Cube); var mat = new Material(Shader.Find("Standard")); mat.SetTexture(propertyName, srcTex); - root.GetComponent().sharedMaterial = mat; + root.GetComponentOrThrow().sharedMaterial = mat; // Export glTF var data = new ExportingGltfData(); diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshExportValidatorTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshExportValidatorTests.cs index 96d2acece..536198182 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshExportValidatorTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshExportValidatorTests.cs @@ -12,7 +12,7 @@ namespace UniGLTF var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.SetParent(root.transform); - var renderer = cube.GetComponent(); + var renderer = cube.GetComponentOrThrow(); renderer.sharedMaterials = materials; return root; } @@ -88,7 +88,10 @@ namespace UniGLTF var child = GameObject.CreatePrimitive(PrimitiveType.Cube); child.transform.SetParent(root.transform); // remove MeshFilter - Component.DestroyImmediate(child.GetComponent()); + if (child.TryGetComponent(out var mf)) + { + Component.DestroyImmediate(mf); + } validator.SetRoot(root, new GltfExportSettings(), new DefualtBlendShapeExportFilter()); var vs = validator.Validate(root); @@ -112,7 +115,7 @@ namespace UniGLTF var child = GameObject.CreatePrimitive(PrimitiveType.Cube); child.transform.SetParent(root.transform); // set null - child.GetComponent().sharedMesh = null; + child.GetComponentOrThrow().sharedMesh = null; validator.SetRoot(root, new GltfExportSettings(), new DefualtBlendShapeExportFilter()); var vs = validator.Validate(root); diff --git a/Assets/UniGLTF_Samples/UniHumanoid/HumanBuilderTest.cs b/Assets/UniGLTF_Samples/UniHumanoid/HumanBuilderTest.cs index 6c81c802d..188eb73f3 100644 --- a/Assets/UniGLTF_Samples/UniHumanoid/HumanBuilderTest.cs +++ b/Assets/UniGLTF_Samples/UniHumanoid/HumanBuilderTest.cs @@ -120,6 +120,10 @@ namespace UniHumanoid var description = AvatarDescription.Create(builder.Skeleton); var animator = GetComponent(); + if (animator == null) + { + throw new System.ArgumentException("no animator"); + } animator.avatar = description.CreateAvatar(root); // create SkinnedMesh for bone visualize diff --git a/Assets/VRM/Editor/Format/VRMBlendShapeExportFilter.cs b/Assets/VRM/Editor/Format/VRMBlendShapeExportFilter.cs index d1a754262..363ce7974 100644 --- a/Assets/VRM/Editor/Format/VRMBlendShapeExportFilter.cs +++ b/Assets/VRM/Editor/Format/VRMBlendShapeExportFilter.cs @@ -16,7 +16,7 @@ namespace VRM { continue; } - + if (onlyPreset) { if (c.Preset == BlendShapePreset.Unknown) @@ -45,8 +45,7 @@ namespace VRM Clips = new List(); if (exportRoot != null) { - var proxy = exportRoot.GetComponent(); - if (proxy != null) + if (exportRoot.TryGetComponent(out var proxy)) { if (proxy.BlendShapeAvatar != null) { diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index f4b30ed58..a51df4bfc 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -153,7 +153,7 @@ namespace VRM target = GameObject.Instantiate(target); destroy.Add(target); - var metaBehaviour = target.GetComponent(); + var metaBehaviour = target.GetComponentOrNull(); if (metaBehaviour == null) { metaBehaviour = target.AddComponent(); @@ -167,7 +167,7 @@ namespace VRM { // copy元 - var animator = exportRoot.GetComponent(); + var animator = exportRoot.GetComponentOrNull(); var beforeTransforms = exportRoot.GetComponentsInChildren(true); // copy先 var afterTransforms = target.GetComponentsInChildren(true); @@ -204,11 +204,8 @@ namespace VRM VRMBoneNormalizer.Execute(target, settings.ForceTPose); } - var fp = target.GetComponent(); - // 元のBlendShapeClipに変更を加えないように複製 - var proxy = target.GetComponent(); - if (proxy != null) + if (target.TryGetComponent(out var proxy)) { var copyBlendShapeAvatar = CopyBlendShapeAvatar(proxy.BlendShapeAvatar, settings.ReduceBlendshapeClip); proxy.BlendShapeAvatar = copyBlendShapeAvatar; diff --git a/Assets/VRM/Editor/Format/VRMExporterVaildator.cs b/Assets/VRM/Editor/Format/VRMExporterVaildator.cs index a8fd290cb..6fc891b66 100644 --- a/Assets/VRM/Editor/Format/VRMExporterVaildator.cs +++ b/Assets/VRM/Editor/Format/VRMExporterVaildator.cs @@ -23,13 +23,12 @@ namespace VRM yield break; } - if (ReduceBlendshape && ExportRoot.GetComponent() == null) + if (ReduceBlendshape && ExportRoot.GetComponentOrNull() == null) { yield return Validation.Error(VRMExporterWizardMessages.NEEDS_VRM_BLENDSHAPE_PROXY.Msg()); } - var vrmMeta = ExportRoot.GetComponent(); - if (vrmMeta != null && vrmMeta.Meta != null && vrmMeta.Meta.Thumbnail != null) + if (ExportRoot.TryGetComponent(out var vrmMeta) && vrmMeta.Meta != null && vrmMeta.Meta.Thumbnail != null) { var thumbnailName = vrmMeta.Meta.Thumbnail.name; if (NameValidator.IsFileNameLengthTooLong(thumbnailName)) diff --git a/Assets/VRM/Editor/Format/VRMExporterWizard.cs b/Assets/VRM/Editor/Format/VRMExporterWizard.cs index 4e89b0e92..cb2a44768 100644 --- a/Assets/VRM/Editor/Format/VRMExporterWizard.cs +++ b/Assets/VRM/Editor/Format/VRMExporterWizard.cs @@ -79,8 +79,7 @@ namespace VRM } else { - var meta = root.GetComponent(); - if (meta != null) + if (root.TryGetComponent(out var meta)) { Meta = meta.Meta; } @@ -150,14 +149,12 @@ namespace VRM yield return VRMSpringBoneValidator.Validate; - var firstPerson = State.ExportRoot.GetComponent(); - if (firstPerson != null) + if (State.ExportRoot.TryGetComponent(out var firstPerson)) { yield return firstPerson.Validate; } - var proxy = State.ExportRoot.GetComponent(); - if (proxy != null) + if (State.ExportRoot.TryGetComponent(out var proxy)) { yield return proxy.Validate; } @@ -183,7 +180,7 @@ namespace VRM // // T-Pose // - if (State.ExportRoot.GetComponent() != null) + if (State.ExportRoot.TryGetComponent(out var animator)) { var backup = GUI.enabled; GUI.enabled = State.ExportRoot.scene.IsValid(); @@ -305,7 +302,7 @@ namespace VRM case Tabs.BlendShape: if (State.ExportRoot) { - OnBlendShapeGUI(State.ExportRoot.GetComponent()); + OnBlendShapeGUI(State.ExportRoot.GetComponentOrNull()); } break; diff --git a/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs b/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs index 777de6e79..274dd48e5 100644 --- a/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs +++ b/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs @@ -17,29 +17,30 @@ namespace VRM return false; } - var animator = root.GetComponent(); - if (animator == null) + if (root.TryGetComponent(out var animator)) + { + var avatar = animator.avatar; + if (avatar == null) + { + return false; + } + + if (!avatar.isValid) + { + return false; + } + + if (!avatar.isHuman) + { + return false; + } + + return true; + } + else { return false; } - - var avatar = animator.avatar; - if (avatar == null) - { - return false; - } - - if (!avatar.isValid) - { - return false; - } - - if (!avatar.isHuman) - { - return false; - } - - return true; } public static void Normalize() diff --git a/Assets/VRM/Editor/LookAt/VRMLookAtHeadEditor.cs b/Assets/VRM/Editor/LookAt/VRMLookAtHeadEditor.cs index 9fa73a5c2..6c77f65d6 100644 --- a/Assets/VRM/Editor/LookAt/VRMLookAtHeadEditor.cs +++ b/Assets/VRM/Editor/LookAt/VRMLookAtHeadEditor.cs @@ -39,10 +39,7 @@ namespace VRM { m_items = m_target.transform.Traverse().Select(x => { - var meshFilter = x.GetComponent(); - var meshRenderer = x.GetComponent(); - var skinnedMeshRenderer = x.GetComponent(); - if (meshFilter != null && meshRenderer != null) + if (x.TryGetComponent(out var meshFilter) && x.TryGetComponent(out var meshRenderer)) { return new Item { @@ -51,7 +48,7 @@ namespace VRM Materials = meshRenderer.sharedMaterials, }; } - else if (skinnedMeshRenderer != null) + else if (x.TryGetComponent(out var skinnedMeshRenderer)) { return new Item { @@ -114,11 +111,11 @@ namespace VRM target.position + new Vector3(0, 0.1f, 0), target.forward ); - for(int j=0; j results) { var clips = new List(); - var proxy = root.GetComponent(); - if (proxy == null || proxy.BlendShapeAvatar == null) + if (root.TryGetComponent(out var proxy)) + { + if (proxy.BlendShapeAvatar == null) + { + return clips; + } + + var util = new VrmBlendShapeUpdater(root, results); + + // create modified BlendShapeClip + var clipAssetPathList = new List(); + foreach (var src in proxy.BlendShapeAvatar.Clips.Where(x => x != null)) + { + var copy = util.RecreateBlendShapeClip(src, assetFolder); + var assetPath = $"{assetFolder}/{copy.name}.asset"; + AssetDatabase.CreateAsset(copy, assetPath); + clipAssetPathList.Add(assetPath); + clips.Add(copy); + } + + // create BlendShapeAvatar + proxy.BlendShapeAvatar = RecreateBlendShapeAvatar(clips, assetFolder); + + return clips; + } + else { return clips; } - - var util = new VrmBlendShapeUpdater(root, results); - - // create modified BlendShapeClip - var clipAssetPathList = new List(); - foreach (var src in proxy.BlendShapeAvatar.Clips.Where(x => x != null)) - { - var copy = util.RecreateBlendShapeClip(src, assetFolder); - var assetPath = $"{assetFolder}/{copy.name}.asset"; - AssetDatabase.CreateAsset(copy, assetPath); - clipAssetPathList.Add(assetPath); - clips.Add(copy); - } - - // create BlendShapeAvatar - proxy.BlendShapeAvatar = RecreateBlendShapeAvatar(clips, assetFolder); - - return clips; } BlendShapeClip RecreateBlendShapeClip(BlendShapeClip src, string assetFolder) diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/VrmMeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/VrmMeshIntegratorWizard.cs index d6fe6949a..4138cb501 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/VrmMeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/VrmMeshIntegratorWizard.cs @@ -21,7 +21,7 @@ namespace VRM protected override void Validate() { base.Validate(); - if (_exportTarget.GetComponent() == null) + if (_exportTarget.GetComponentOrNull() == null) { _validations.Add(Validation.Error("target is not vrm1")); return; @@ -60,7 +60,7 @@ namespace VRM base.WriteAssets(assetFolder, instance, results); // reset firstPerson - if (instance.GetComponent() is VRMFirstPerson firstPerson) + if (instance.TryGetComponent(out var firstPerson)) { // TODO: firstPerson.Reset(); diff --git a/Assets/VRM/Editor/SpringBone/VRMSpringBoneUtilityEditor.cs b/Assets/VRM/Editor/SpringBone/VRMSpringBoneUtilityEditor.cs index 30f5b932e..9380f20e3 100644 --- a/Assets/VRM/Editor/SpringBone/VRMSpringBoneUtilityEditor.cs +++ b/Assets/VRM/Editor/SpringBone/VRMSpringBoneUtilityEditor.cs @@ -19,8 +19,7 @@ namespace VRM return false; } - var animator = root.GetComponent(); - if (animator == null) + if (root.TryGetComponent(out var animator)) { return false; } @@ -66,8 +65,7 @@ namespace VRM return false; } - var animator = root.GetComponent(); - if (animator == null) + if (root.TryGetComponent(out var animator)) { return false; } diff --git a/Assets/VRM/Runtime/BlendShape/BlendShapeBindingMerger.cs b/Assets/VRM/Runtime/BlendShape/BlendShapeBindingMerger.cs index 0dbfd1745..220fdeb60 100644 --- a/Assets/VRM/Runtime/BlendShape/BlendShapeBindingMerger.cs +++ b/Assets/VRM/Runtime/BlendShape/BlendShapeBindingMerger.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using UniGLTF; using UnityEngine; namespace VRM @@ -51,7 +52,7 @@ namespace VRM SkinnedMeshRenderer target = null; if (_target != null) { - target = _target.GetComponent(); + target = _target.GetComponentOrNull(); } if (target != null) { diff --git a/Assets/VRM/Runtime/BlendShape/BlendShapeClipHandler.cs b/Assets/VRM/Runtime/BlendShape/BlendShapeClipHandler.cs index 2021df4af..57a5c631b 100644 --- a/Assets/VRM/Runtime/BlendShape/BlendShapeClipHandler.cs +++ b/Assets/VRM/Runtime/BlendShape/BlendShapeClipHandler.cs @@ -1,6 +1,7 @@ using UnityEngine; using System.Linq; using System; +using UniGLTF; namespace VRM { @@ -28,7 +29,7 @@ namespace VRM m_renderers = m_clip.Values.Select(x => { var target = UniGLTF.UnityExtensions.GetFromPath(transform, x.RelativePath); - return target.GetComponent(); + return target.GetComponentOrNull(); }) .ToArray(); } diff --git a/Assets/VRM/Runtime/BlendShape/Blinker.cs b/Assets/VRM/Runtime/BlendShape/Blinker.cs index 4be359f78..61052f97b 100644 --- a/Assets/VRM/Runtime/BlendShape/Blinker.cs +++ b/Assets/VRM/Runtime/BlendShape/Blinker.cs @@ -1,6 +1,7 @@ using System.Collections; using UnityEngine; using UnityEngine.Serialization; +using UniGLTF; namespace VRM { @@ -100,7 +101,7 @@ namespace VRM private void OnEnable() { - m_blendShapes = GetComponent(); + m_blendShapes = this.GetComponentOrNull(); m_coroutine = StartCoroutine(BlinkRoutine()); } diff --git a/Assets/VRM/Runtime/BlendShape/MaterialValueBindingMerger.cs b/Assets/VRM/Runtime/BlendShape/MaterialValueBindingMerger.cs index 0dcb380a3..59959bd0a 100644 --- a/Assets/VRM/Runtime/BlendShape/MaterialValueBindingMerger.cs +++ b/Assets/VRM/Runtime/BlendShape/MaterialValueBindingMerger.cs @@ -52,8 +52,7 @@ namespace VRM foreach (var x in root.Traverse()) { - var renderer = x.GetComponent(); - if (renderer != null) + if (x.TryGetComponent(out var renderer)) { foreach (var y in renderer.sharedMaterials.Where(y => y != null)) { @@ -231,8 +230,8 @@ namespace VRM { return new MaterialTarget { - MaterialName=binding.MaterialName, - ValueName=binding.ValueName + MaterialName = binding.MaterialName, + ValueName = binding.ValueName }; } } @@ -260,7 +259,7 @@ namespace VRM if (valueName.EndsWith("_ST_S")) { valueName = valueName.Substring(0, valueName.Length - 2); - var v=material.GetVector(valueName); + var v = material.GetVector(valueName); value.y = v.y; value.w = v.w; } diff --git a/Assets/VRM/Runtime/BlendShape/MeshPreviewItem.cs b/Assets/VRM/Runtime/BlendShape/MeshPreviewItem.cs index 54dfc182f..07c038b89 100644 --- a/Assets/VRM/Runtime/BlendShape/MeshPreviewItem.cs +++ b/Assets/VRM/Runtime/BlendShape/MeshPreviewItem.cs @@ -190,12 +190,7 @@ namespace VRM public static MeshPreviewItem Create(Transform t, Transform root, Func getOrCreateMaterial) { - //Debug.Log("create"); - - var meshFilter = t.GetComponent(); - var meshRenderer = t.GetComponent(); - var skinnedMeshRenderer = t.GetComponent(); - if (meshFilter != null && meshRenderer != null) + if (t.TryGetComponent(out var meshFilter) && t.TryGetComponent(out var meshRenderer)) { // copy meshRenderer.sharedMaterials = meshRenderer.sharedMaterials.Select(x => getOrCreateMaterial(x)).ToArray(); @@ -204,7 +199,7 @@ namespace VRM Mesh = meshFilter.sharedMesh }; } - else if (skinnedMeshRenderer != null) + else if (t.TryGetComponent(out var skinnedMeshRenderer)) { // copy skinnedMeshRenderer.sharedMaterials = skinnedMeshRenderer.sharedMaterials.Select(x => getOrCreateMaterial(x)).ToArray(); diff --git a/Assets/VRM/Runtime/BlendShape/PreviewSceneManager.cs b/Assets/VRM/Runtime/BlendShape/PreviewSceneManager.cs index f7ea2d16f..58bcfc4d1 100644 --- a/Assets/VRM/Runtime/BlendShape/PreviewSceneManager.cs +++ b/Assets/VRM/Runtime/BlendShape/PreviewSceneManager.cs @@ -122,8 +122,7 @@ namespace VRM .Select(x => x.Path) .ToArray(); - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { var head = animator.GetBoneTransform(HumanBodyBones.Head); if (head != null) diff --git a/Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs b/Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs index 73bb8e6a3..a34cdc1e8 100644 --- a/Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs +++ b/Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs @@ -43,42 +43,43 @@ namespace VRM Debug.LogWarning($"fall back '{binding.RelativePath}' => '{found.RelativePathFrom(root)}'"); } } - var renderer = found.GetComponent(); - if (renderer == null) + if (found.TryGetComponent(out var renderer)) + { + if (!renderer.gameObject.activeInHierarchy) + { + return null; + } + + var mesh = renderer.sharedMesh; + var meshIndex = exporter.Meshes.IndexOf(mesh); + if (meshIndex == -1) + { + return null; + } + + if (!exporter.MeshBlendShapeIndexMap.TryGetValue(mesh, out Dictionary blendShapeIndexMap)) + { + // この Mesh は エクスポートされていない + return null; + } + + if (!blendShapeIndexMap.TryGetValue(binding.Index, out int blendShapeIndex)) + { + // この blendShape は エクスポートされていない(空だった?) + return null; + } + + return new glTF_VRM_BlendShapeBind + { + mesh = meshIndex, + index = blendShapeIndex, + weight = binding.Weight, + }; + } + else { return null; } - - if (!renderer.gameObject.activeInHierarchy) - { - return null; - } - - var mesh = renderer.sharedMesh; - var meshIndex = exporter.Meshes.IndexOf(mesh); - if (meshIndex == -1) - { - return null; - } - - if (!exporter.MeshBlendShapeIndexMap.TryGetValue(mesh, out Dictionary blendShapeIndexMap)) - { - // この Mesh は エクスポートされていない - return null; - } - - if (!blendShapeIndexMap.TryGetValue(binding.Index, out int blendShapeIndex)) - { - // この blendShape は エクスポートされていない(空だった?) - return null; - } - - return new glTF_VRM_BlendShapeBind - { - mesh = meshIndex, - index = blendShapeIndex, - weight = binding.Weight, - }; } public static void Add(this glTF_VRM_BlendShapeMaster master, diff --git a/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs b/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs index 9f0ba8865..43e437968 100644 --- a/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs +++ b/Assets/VRM/Runtime/FirstPerson/VRMFirstPerson.cs @@ -38,8 +38,7 @@ namespace VRM return renderer.sharedMesh; } - var filter = Renderer.GetComponent(); - if (filter != null) + if (Renderer.TryGetComponent(out var filter)) { return filter.sharedMesh; } @@ -75,7 +74,7 @@ namespace VRM .Select(x => { var mapped = map[x.Renderer.transform]; - var renderer = mapped.GetComponent(); + var renderer = mapped.GetComponentOrNull(); return new VRMFirstPerson.RendererFirstPersonFlags { Renderer = renderer, @@ -87,8 +86,7 @@ namespace VRM public void SetDefault() { FirstPersonOffset = new Vector3(0, 0.06f, 0); - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { FirstPersonBone = animator.GetBoneTransform(HumanBodyBones.Head); } diff --git a/Assets/VRM/Runtime/Humanoid/VRMHumanoidDescription.cs b/Assets/VRM/Runtime/Humanoid/VRMHumanoidDescription.cs index 5a177e508..89bdf6957 100644 --- a/Assets/VRM/Runtime/Humanoid/VRMHumanoidDescription.cs +++ b/Assets/VRM/Runtime/Humanoid/VRMHumanoidDescription.cs @@ -32,7 +32,7 @@ namespace VRM private void OnValidate() { - if(Avatar!=null && (!Avatar.isValid || !Avatar.isHuman)) + if (Avatar != null && (!Avatar.isValid || !Avatar.isHuman)) { Avatar = null; } @@ -40,16 +40,13 @@ namespace VRM void Reset() { - var animator = GetComponent(); - if (animator == null) + if (TryGetComponent(out var animator)) { - return; - } - - Avatar = animator.avatar; - if (Avatar == null) - { - return; + Avatar = animator.avatar; + if (Avatar == null) + { + return; + } } } } diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 93c76757f..5497b1b3e 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -48,19 +48,19 @@ namespace VRM public override void ExportExtensions(ITextureSerializer textureSerializer) { // avatar - var animator = Copy.GetComponent(); - if (animator != null) + if (Copy.TryGetComponent(out var animator)) { - var humanoid = Copy.GetComponent(); UniHumanoid.AvatarDescription description = null; - var nodes = Copy.transform.Traverse().Skip(1).ToList(); + var isCreated = false; + if (Copy.TryGetComponent(out var humanoid)) { - var isCreated = false; if (humanoid != null) { description = humanoid.GetDescription(out isCreated); } - + } + var nodes = Copy.transform.Traverse().Skip(1).ToList(); + { if (description != null) { // use description @@ -101,8 +101,7 @@ namespace VRM } // morph - var master = Copy.GetComponent(); - if (master != null) + if (Copy.TryGetComponent(out var master)) { var avatar = master.BlendShapeAvatar; if (avatar != null) @@ -127,8 +126,7 @@ namespace VRM #pragma warning disable 0618 // meta(obsolete) { - var meta = Copy.GetComponent(); - if (meta != null) + if (Copy.TryGetComponent(out var meta)) { VRM.meta.author = meta.Author; VRM.meta.contactInformation = meta.ContactInformation; @@ -147,8 +145,7 @@ namespace VRM // meta { - var _meta = Copy.GetComponent(); - if (_meta != null && _meta.Meta != null) + if (Copy.TryGetComponent(out var _meta) && _meta.Meta != null) { var meta = _meta.Meta; @@ -180,8 +177,7 @@ namespace VRM } // firstPerson - var firstPerson = Copy.GetComponent(); - if (firstPerson != null) + if (Copy.TryGetComponent(out var firstPerson)) { if (firstPerson.FirstPersonBone != null) { @@ -195,27 +191,22 @@ namespace VRM } // lookAt + if (Copy.TryGetComponent(out var lookAtHead)) { - var lookAtHead = Copy.GetComponent(); - if (lookAtHead != null) + if (Copy.TryGetComponent(out var boneApplyer)) { - var boneApplyer = Copy.GetComponent(); - var blendShapeApplyer = Copy.GetComponent(); - if (boneApplyer != null) - { - VRM.firstPerson.lookAtType = LookAtType.Bone; - VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner); - VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter); - VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown); - VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp); - } - else if (blendShapeApplyer != null) - { - VRM.firstPerson.lookAtType = LookAtType.BlendShape; - VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal); - VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown); - VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp); - } + VRM.firstPerson.lookAtType = LookAtType.Bone; + VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner); + VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter); + VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown); + VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp); + } + else if (Copy.TryGetComponent(out var blendShapeApplyer)) + { + VRM.firstPerson.lookAtType = LookAtType.BlendShape; + VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal); + VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown); + VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp); } } } diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 9b1ee9683..642c40917 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -268,11 +268,7 @@ namespace VRM humanoid.Avatar = HumanoidAvatar; humanoid.Description = AvatarDescription; - var animator = Root.GetComponent(); - if (animator == null) - { - animator = Root.AddComponent(); - } + var animator = Root.GetOrAddComponent(); animator.avatar = HumanoidAvatar; // default としてとりあえず設定する diff --git a/Assets/VRM/Runtime/LookAt/VRMLookAt.cs b/Assets/VRM/Runtime/LookAt/VRMLookAt.cs index 419a0f7bc..f99c63653 100644 --- a/Assets/VRM/Runtime/LookAt/VRMLookAt.cs +++ b/Assets/VRM/Runtime/LookAt/VRMLookAt.cs @@ -115,8 +115,7 @@ namespace VRM public void GetBones() { - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { LeftEye = OffsetOnTransform.Create(animator.GetBoneTransform(HumanBodyBones.LeftEye)); RightEye = OffsetOnTransform.Create(animator.GetBoneTransform(HumanBodyBones.RightEye)); diff --git a/Assets/VRM/Runtime/LookAt/VRMLookAtBlendShapeApplyer.cs b/Assets/VRM/Runtime/LookAt/VRMLookAtBlendShapeApplyer.cs index b605fbc66..16c1b99b8 100644 --- a/Assets/VRM/Runtime/LookAt/VRMLookAtBlendShapeApplyer.cs +++ b/Assets/VRM/Runtime/LookAt/VRMLookAtBlendShapeApplyer.cs @@ -1,4 +1,5 @@ #pragma warning disable 0414, 0649 +using UniGLTF; using UnityEngine; @@ -46,8 +47,8 @@ namespace VRM private void Start() { - m_head = GetComponent(); - m_proxy = GetComponent(); + m_head = this.GetComponentOrNull(); + m_proxy = this.GetComponentOrNull(); if (m_head == null) { enabled = false; diff --git a/Assets/VRM/Runtime/LookAt/VRMLookAtBoneApplyer.cs b/Assets/VRM/Runtime/LookAt/VRMLookAtBoneApplyer.cs index 9dc050066..dae52960b 100644 --- a/Assets/VRM/Runtime/LookAt/VRMLookAtBoneApplyer.cs +++ b/Assets/VRM/Runtime/LookAt/VRMLookAtBoneApplyer.cs @@ -29,8 +29,7 @@ namespace VRM public void OnImported(VRMImporterContext context) { - var animator = GetComponent(); - if (animator != null) + if (TryGetComponent(out var animator)) { LeftEye = OffsetOnTransform.Create(animator.GetBoneTransform(HumanBodyBones.LeftEye)); RightEye = OffsetOnTransform.Create(animator.GetBoneTransform(HumanBodyBones.RightEye)); @@ -55,7 +54,7 @@ namespace VRM void Start() { - m_head = GetComponent(); + m_head = this.GetComponentOrNull(); if (m_head == null) { enabled = false; diff --git a/Assets/VRM/Runtime/LookAt/VRMLookAtHead.cs b/Assets/VRM/Runtime/LookAt/VRMLookAtHead.cs index ddbd84110..6073de323 100644 --- a/Assets/VRM/Runtime/LookAt/VRMLookAtHead.cs +++ b/Assets/VRM/Runtime/LookAt/VRMLookAtHead.cs @@ -80,7 +80,7 @@ namespace VRM void Awake() { - var animator = GetComponent(); + var animator = this.GetComponentOrNull(); if (animator == null) { Debug.LogWarning("animator is not found"); diff --git a/Assets/VRM/Runtime/Meta/VRMMetaInformation.cs b/Assets/VRM/Runtime/Meta/VRMMetaInformation.cs index 8eaca37bd..72b023889 100644 --- a/Assets/VRM/Runtime/Meta/VRMMetaInformation.cs +++ b/Assets/VRM/Runtime/Meta/VRMMetaInformation.cs @@ -54,8 +54,7 @@ namespace VRM [ContextMenu("CreateThumbnail")] void CreateThumbnailMenu() { - var lookAt = GetComponent(); - if (lookAt != null) + if(TryGetComponent(out var lookAt)) { var texture = lookAt.CreateThumbnail(); diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs index f7b19239d..e8e4cde50 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs @@ -14,11 +14,7 @@ namespace VRM { public static void EnforceTPose(GameObject go) { - var animator = go.GetComponent(); - if (animator == null) - { - throw new ArgumentException("Animator with avatar is required"); - } + var animator = go.GetComponentOrThrow(); var avatar = animator.avatar; if (avatar == null) @@ -61,7 +57,7 @@ namespace VRM if (forceTPose) { // T-Poseにする - var hips = go.GetComponent().GetBoneTransform(HumanBodyBones.Hips); + var hips = go.GetComponentOrThrow().GetBoneTransform(HumanBodyBones.Hips); var hipsPosition = hips.position; var hipsRotation = hips.rotation; try @@ -85,21 +81,23 @@ namespace VRM BoneNormalizer.Replace(go, newMeshMap, true, true); // 回転とスケールが除去された新しいヒエラルキーからAvatarを作る - var animator = go.GetComponent(); - var newAvatar = UniHumanoid.AvatarDescription.RecreateAvatar(animator); - - // Animator.avatar を代入したときに副作用でTransformが変更されるのを回避するために削除します。 - if (Application.isPlaying) + Avatar newAvatar = default; + if (go.TryGetComponent(out var animator)) { - GameObject.Destroy(animator); - } - else - { - GameObject.DestroyImmediate(animator); - } - animator = go.AddComponent(); + newAvatar = UniHumanoid.AvatarDescription.RecreateAvatar(animator); - animator.avatar = newAvatar; + // Animator.avatar を代入したときに副作用でTransformが変更されるのを回避するために削除します。 + if (Application.isPlaying) + { + GameObject.Destroy(animator); + } + else + { + GameObject.DestroyImmediate(animator); + } + } + + go.AddComponent().avatar = newAvatar; } /// @@ -111,10 +109,9 @@ namespace VRM static void CopyVRMComponents(GameObject go, GameObject root, Dictionary map) { + // blendshape { - // blendshape - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { var dst = root.AddComponent(); dst.BlendShapeAvatar = src.BlendShapeAvatar; @@ -172,7 +169,7 @@ namespace VRM if (src.ColliderGroups != null) { dst.ColliderGroups = src.ColliderGroups - .Select(x => map[x.transform].GetComponent()).ToArray(); + .Select(x => map[x.transform].GetComponentOrThrow()).ToArray(); } } } @@ -180,8 +177,7 @@ namespace VRM #pragma warning disable 0618 { // meta(obsolete) - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { src.CopyTo(root); } @@ -190,8 +186,7 @@ namespace VRM { // meta - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { var dst = root.AddComponent(); dst.Meta = src.Meta; @@ -200,24 +195,21 @@ namespace VRM { // firstPerson - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { src.CopyTo(root, map); } } { // look at - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { var dst = root.AddComponent(); } } { // bone applier - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { var dst = root.AddComponent(); dst.HorizontalInner.Assign(src.HorizontalInner); @@ -228,8 +220,7 @@ namespace VRM } { // blendshape applier - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { var dst = root.AddComponent(); dst.Horizontal.Assign(src.Horizontal); @@ -241,16 +232,14 @@ namespace VRM { // humanoid var dst = root.AddComponent(); - var src = go.GetComponent(); - if (src != null) + if (go.TryGetComponent(out var src)) { dst.Avatar = src.Avatar; dst.Description = src.Description; } else { - var animator = go.GetComponent(); - if (animator != null) + if (go.TryGetComponent(out var animator)) { dst.Avatar = animator.avatar; } diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs index 49c8d020d..375214498 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VrmMeshUtility.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using UniGLTF; using UniHumanoid; using UnityEngine; using UnityEngine.XR; @@ -97,11 +98,7 @@ namespace VRM public override (List, List) Process( GameObject target, IEnumerable copyGroup) { - _vrmInstance = target.GetComponent(); - if (_vrmInstance == null) - { - throw new ArgumentException(); - } + _vrmInstance = target.GetComponentOrThrow(); // TODO: update: spring // TODO: update: constraint @@ -110,21 +107,23 @@ namespace VRM if (FreezeBlendShapeRotationAndScaling) { - var animator = target.GetComponent(); - var newAvatar = AvatarDescription.RecreateAvatar(animator); - - // ??? clear old avatar ??? - var t = animator.gameObject; - if (Application.isPlaying) + Avatar newAvatar = null; + if (target.TryGetComponent(out var animator)) { - GameObject.Destroy(animator); - } - else - { - GameObject.DestroyImmediate(animator); + newAvatar = AvatarDescription.RecreateAvatar(animator); + // ??? clear old avatar ??? + var t = animator.gameObject; + if (Application.isPlaying) + { + GameObject.Destroy(animator); + } + else + { + GameObject.DestroyImmediate(animator); + } } - t.AddComponent().avatar = newAvatar; + target.AddComponent().avatar = newAvatar; } return (list, newList); @@ -137,23 +136,26 @@ namespace VRM { return; } - var vrm0 = root.GetComponent(); - if (vrm0 == null) + + if (root.TryGetComponent(out var vrm0)) + { + foreach (var a in vrm0.Renderers) + { + var g = _GetOrCreateGroup(a.FirstPersonFlag.ToString()); + g.Renderers.Add(a.Renderer); + } + + var orphan = root.GetComponentsInChildren().Where(x => !_HasRenderer(x)).ToArray(); + if (orphan.Length > 0) + { + var g = _GetOrCreateGroup("both"); + g.Renderers.AddRange(orphan); + } + } + else { return; } - foreach (var a in vrm0.Renderers) - { - var g = _GetOrCreateGroup(a.FirstPersonFlag.ToString()); - g.Renderers.Add(a.Renderer); - } - - var orphan = root.GetComponentsInChildren().Where(x => !_HasRenderer(x)).ToArray(); - if (orphan.Length > 0) - { - var g = _GetOrCreateGroup("both"); - g.Renderers.AddRange(orphan); - } } } } \ No newline at end of file diff --git a/Assets/VRM/Runtime/SpringBone/VRMSpringUtility.cs b/Assets/VRM/Runtime/SpringBone/VRMSpringUtility.cs index 634d43ebe..7f1c937e5 100644 --- a/Assets/VRM/Runtime/SpringBone/VRMSpringUtility.cs +++ b/Assets/VRM/Runtime/SpringBone/VRMSpringUtility.cs @@ -18,7 +18,7 @@ namespace VRM { var colliders = new List(); foreach (var vrmColliderGroup in root.Traverse() - .Select(x => x.GetComponent()) + .Select(x => x.GetComponentOrNull()) .Where(x => x != null)) { var index = nodes.IndexOf(vrmColliderGroup.transform); diff --git a/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringBoneDrawer.cs b/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringBoneDrawer.cs index bc9bbbfaa..2a05cf802 100644 --- a/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringBoneDrawer.cs +++ b/Assets/VRM10/Editor/Components/SpringBone/VRM10SpringBoneDrawer.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using UnityEditor; using UnityEngine; +using UniGLTF; namespace UniVRM10 { @@ -49,25 +50,18 @@ namespace UniVRM10 if (parent.transform.childCount > 0) { var child = parent.transform.GetChild(0); - var joint = child.GetComponent(); - if (joint == null) - { - joint = child.gameObject.AddComponent(); - } - if (joint != null) - { - // set params - joint.m_dragForce = parent.m_dragForce; - joint.m_gravityDir = parent.m_gravityDir; - joint.m_gravityPower = parent.m_gravityPower; - joint.m_jointRadius = parent.m_jointRadius; - joint.m_stiffnessForce = parent.m_stiffnessForce; + var joint = child.gameObject.GetOrAddComponent(); + // set params + joint.m_dragForce = parent.m_dragForce; + joint.m_gravityDir = parent.m_gravityDir; + joint.m_gravityPower = parent.m_gravityPower; + joint.m_jointRadius = parent.m_jointRadius; + joint.m_stiffnessForce = parent.m_stiffnessForce; - yield return joint; - foreach (var x in MakeJointsRecursive(joint)) - { - yield return x; - } + yield return joint; + foreach (var x in MakeJointsRecursive(joint)) + { + yield return x; } } } diff --git a/Assets/VRM10/Editor/Components/VRM10Window.cs b/Assets/VRM10/Editor/Components/VRM10Window.cs index d9fd1ff62..9256d9fe0 100644 --- a/Assets/VRM10/Editor/Components/VRM10Window.cs +++ b/Assets/VRM10/Editor/Components/VRM10Window.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using UnityEditor; using UnityEngine; +using UniGLTF; namespace UniVRM10 { @@ -17,7 +18,7 @@ namespace UniVRM10 var window = (VRM10Window)GetWindow(typeof(VRM10Window)); window.titleContent = new GUIContent(MENU_NAME); window.Show(); - window.Root = UnityEditor.Selection.activeTransform?.GetComponent(); + window.Root = UnityEditor.Selection.activeTransform?.GetComponentOrNull(); return window; } @@ -89,7 +90,7 @@ namespace UniVRM10 { if (UnityEditor.Selection.activeTransform != null) { - var root = UnityEditor.Selection.activeTransform.Ancestors().Select(x => x.GetComponent()).FirstOrDefault(x => x != null); + var root = UnityEditor.Selection.activeTransform.Ancestors().Select(x => x.GetComponentOrNull()).FirstOrDefault(x => x != null); if (root != null) { Root = root; diff --git a/Assets/VRM10/Editor/EditorTool/VRM10BoneSelectorEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10BoneSelectorEditorTool.cs index 77e582799..250e4b981 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10BoneSelectorEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10BoneSelectorEditorTool.cs @@ -70,7 +70,7 @@ namespace UniVRM10 _impl = new BoneSelector(SceneView.lastActiveSceneView.camera); } - var root = Selection.activeGameObject?.GetComponent(); + var root = Selection.activeGameObject?.GetComponentOrNull(); if (root == null) { return; diff --git a/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs index 296cad655..617e4d772 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10ExpressionEditorTool.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UnityEditor; using UnityEditor.EditorTools; using UnityEngine; +using UniGLTF; #if UNITY_2021_OR_NEWER #else @@ -50,7 +51,7 @@ namespace UniVRM10 { return; } - var root = Selection.activeTransform.GetComponent(); + var root = Selection.activeTransform.GetComponentOrNull(); if (root == null) { return; diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index b98e66db3..adb89f91b 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -1,6 +1,7 @@ using UnityEngine; using UnityEditor; using UnityEditor.EditorTools; +using UniGLTF; #if UNITY_2021_OR_NEWER #else @@ -50,7 +51,7 @@ namespace UniVRM10 { return; } - var root = Selection.activeTransform.GetComponent(); + var root = Selection.activeTransform.GetComponentOrNull(); if (root == null) { return; @@ -59,7 +60,7 @@ namespace UniVRM10 { return; } - var humanoid = root.GetComponent(); + var humanoid = root.GetComponentOrThrow(); var head = humanoid.Head; if (head == null) { diff --git a/Assets/VRM10/Editor/MeshUtility/Vrm10ExpressionUpdater.cs b/Assets/VRM10/Editor/MeshUtility/Vrm10ExpressionUpdater.cs index 05f9ec3f9..e435e1fa0 100644 --- a/Assets/VRM10/Editor/MeshUtility/Vrm10ExpressionUpdater.cs +++ b/Assets/VRM10/Editor/MeshUtility/Vrm10ExpressionUpdater.cs @@ -92,7 +92,7 @@ namespace UniVRM10 public static Dictionary Update(string assetFolder, GameObject instance, List results) { - var vrm = instance.GetComponent(); + var vrm = instance.GetComponentOrThrow(); var util = new Vrm10ExpressionUpdater(instance, results); // write Vrm10Expressions diff --git a/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs b/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs index e96caf019..08818af89 100644 --- a/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs +++ b/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs @@ -21,7 +21,7 @@ namespace UniVRM10 protected override void Validate() { base.Validate(); - if (_exportTarget.GetComponent() == null) + if (_exportTarget.GetComponentOrNull() == null) { _validations.Add(Validation.Error("target is not vrm1")); return; diff --git a/Assets/VRM10/Editor/Vrm10ExportDialog.cs b/Assets/VRM10/Editor/Vrm10ExportDialog.cs index a88d000fe..64b8a6e7a 100644 --- a/Assets/VRM10/Editor/Vrm10ExportDialog.cs +++ b/Assets/VRM10/Editor/Vrm10ExportDialog.cs @@ -82,8 +82,7 @@ namespace UniVRM10 } else { - var controller = root.GetComponent(); - if (controller != null) + if (root.TryGetComponent(out var controller)) { Vrm = controller.Vrm; } @@ -173,7 +172,7 @@ namespace UniVRM10 return false; } - if (State.ExportRoot.GetComponent() != null) + if (State.ExportRoot.TryGetComponent(out var animator)) { var backup = GUI.enabled; GUI.enabled = State.ExportRoot.scene.IsValid(); diff --git a/Assets/VRM10/Editor/Vrm10InstanceEditor.cs b/Assets/VRM10/Editor/Vrm10InstanceEditor.cs index 4438327fe..fa2c984be 100644 --- a/Assets/VRM10/Editor/Vrm10InstanceEditor.cs +++ b/Assets/VRM10/Editor/Vrm10InstanceEditor.cs @@ -64,8 +64,7 @@ namespace UniVRM10 static bool CheckHumanoid(GameObject go) { - var animator = go.GetComponent(); - if (animator != null) + if (go.TryGetComponent(out var animator)) { if (animator.avatar == null) { @@ -87,7 +86,7 @@ namespace UniVRM10 return true; } - var humanoid = go.GetComponent(); + var humanoid = go.GetComponentOrNull(); if (humanoid == null) { EditorGUILayout.HelpBox("vrm-1.0 require Animator or UniHumanoid.Humanoid", MessageType.Error); diff --git a/Assets/VRM10/Editor/VrmAnimationMenu.cs b/Assets/VRM10/Editor/VrmAnimationMenu.cs index 40ef1013f..d30fd127e 100644 --- a/Assets/VRM10/Editor/VrmAnimationMenu.cs +++ b/Assets/VRM10/Editor/VrmAnimationMenu.cs @@ -80,7 +80,7 @@ namespace UniVRM10 // setup // var map = new Dictionary(); - var animator = bvh.Root.GetComponent(); + var animator = bvh.Root.GetComponentOrThrow(); foreach (HumanBodyBones bone in Enum.GetValues(typeof(HumanBodyBones))) { if (bone == HumanBodyBones.LastBone) @@ -107,7 +107,7 @@ namespace UniVRM10 // // get data // - var animation = bvh.Root.gameObject.GetComponent(); + var animation = bvh.Root.gameObject.GetComponentOrThrow(); var clip = animation.clip; var state = animation[clip.name]; diff --git a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger/MorphTargetIdentifier.cs b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger/MorphTargetIdentifier.cs index c230a708b..4285d4cd7 100644 --- a/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger/MorphTargetIdentifier.cs +++ b/Assets/VRM10/Runtime/Components/Expression/MorphTargetBindingMerger/MorphTargetIdentifier.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using UniGLTF; using UnityEngine; namespace UniVRM10 @@ -13,7 +14,7 @@ namespace UniVRM10 var targetGameObject = modelRoot.Find(binding.RelativePath); if (targetGameObject == null) return null; - var targetRenderer = targetGameObject.GetComponent(); + var targetRenderer = targetGameObject.GetComponentOrNull(); if (targetRenderer == null) return null; if (targetRenderer.sharedMesh == null) return null; if (targetRenderer.sharedMesh.blendShapeCount <= binding.Index) return null; diff --git a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs index 913f9cf53..86cc0e050 100644 --- a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs +++ b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewMeshItem.cs @@ -97,12 +97,7 @@ namespace UniVRM10 public static PreviewMeshItem Create(Transform t, Transform root, Func getOrCreateMaterial) { - //Debug.Log("create"); - - var meshFilter = t.GetComponent(); - var meshRenderer = t.GetComponent(); - var skinnedMeshRenderer = t.GetComponent(); - if (meshFilter != null && meshRenderer != null) + if (t.TryGetComponent(out var meshFilter) && t.TryGetComponent(out var meshRenderer)) { // copy meshRenderer.sharedMaterials = meshRenderer.sharedMaterials.Select(x => getOrCreateMaterial(x)).ToArray(); @@ -111,7 +106,7 @@ namespace UniVRM10 Mesh = meshFilter.sharedMesh }; } - else if (skinnedMeshRenderer != null) + else if (t.TryGetComponent(out var skinnedMeshRenderer)) { // copy skinnedMeshRenderer.sharedMaterials = skinnedMeshRenderer.sharedMaterials.Select(x => getOrCreateMaterial(x)).ToArray(); diff --git a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs index a92c35f92..f9b293197 100644 --- a/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs +++ b/Assets/VRM10/Runtime/Components/Expression/Preview/PreviewSceneManager.cs @@ -138,8 +138,7 @@ namespace UniVRM10 .Select(x => x.Path) .ToArray(); - var animator = GetComponent(); - if (animator != null) + if(TryGetComponent(out var animator)) { var head = animator.GetBoneTransform(HumanBodyBones.Head); if (head != null) diff --git a/Assets/VRM10/Runtime/Components/FirstPerson/RendererFirstPersonFlags.cs b/Assets/VRM10/Runtime/Components/FirstPerson/RendererFirstPersonFlags.cs index c7c097d79..d9183a28f 100644 --- a/Assets/VRM10/Runtime/Components/FirstPerson/RendererFirstPersonFlags.cs +++ b/Assets/VRM10/Runtime/Components/FirstPerson/RendererFirstPersonFlags.cs @@ -1,4 +1,5 @@ using System; +using UniGLTF; using UnityEngine; namespace UniVRM10 @@ -27,7 +28,7 @@ namespace UniVRM10 public Renderer GetRenderer(Transform root) { var node = root.Find(Renderer); - return node?.GetComponent(); + return node?.GetComponentOrNull(); } public Mesh GetSharedMesh(Transform root) @@ -39,8 +40,7 @@ namespace UniVRM10 return smr.sharedMesh; case MeshRenderer meshRenderer: - var filter = renderer.GetComponent(); - if (filter != null) + if (renderer.TryGetComponent(out var filter)) { return filter.sharedMesh; } diff --git a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs index a86d9db01..4583e9fdb 100644 --- a/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs +++ b/Assets/VRM10/Runtime/Components/SpringBone/VRM10SpringBoneJoint.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using UniGLTF; using UnityEngine; @@ -29,12 +30,7 @@ namespace UniVRM10 void AddJointRecursive(Transform t, VRM10SpringBoneJoint src) { - var joint = t.gameObject.GetComponent(); - if (joint == null) - { - joint = t.gameObject.AddComponent(); - Debug.Log($"{joint} added"); - } + var joint = t.gameObject.GetOrAddComponent(); // copy settings joint.m_stiffnessForce = src.m_stiffnessForce; @@ -52,8 +48,7 @@ namespace UniVRM10 void GetJoints(Transform t, List joints) { - var joint = t.GetComponent(); - if (joint != null) + if (t.TryGetComponent(out var joint)) { joints.Add(joint); } diff --git a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectFirstPerson.cs b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectFirstPerson.cs index acb6ffd79..db97f8c64 100644 --- a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectFirstPerson.cs +++ b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectFirstPerson.cs @@ -176,8 +176,8 @@ namespace UniVRM10 } m_done = true; - var runtime = go.GetComponent(); - var vrmInstance = go.GetComponent(); + var runtime = go.GetComponentOrThrow(); + var vrmInstance = go.GetComponentOrThrow(); // NOTE: This bone must be referenced by renderers instead of the control rig bone. var firstPersonBone = vrmInstance.Humanoid.Head; diff --git a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs index 8048544a3..7bf5c4a5e 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using UniGLTF; using UnityEngine; using UnityEngine.Serialization; @@ -81,7 +82,7 @@ namespace UniVRM10 { if (m_humanoid == null) { - m_humanoid = GetComponent(); + m_humanoid = this.GetComponentOrNull(); } return m_humanoid; } diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/ControlRig/Vrm10RuntimeControlRig.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/ControlRig/Vrm10RuntimeControlRig.cs index d054033e8..9c6e07226 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/ControlRig/Vrm10RuntimeControlRig.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/ControlRig/Vrm10RuntimeControlRig.cs @@ -41,8 +41,11 @@ namespace UniVRM10 _controlRigAvatar = HumanoidLoader.LoadHumanoidAvatar(vrmRoot, transformBonePairs); _controlRigAvatar.name = "Runtime Control Rig"; - ControlRigAnimator = vrmRoot.GetComponent(); - ControlRigAnimator.avatar = _controlRigAvatar; + if (vrmRoot.TryGetComponent(out var animator)) + { + ControlRigAnimator = animator; + ControlRigAnimator.avatar = _controlRigAvatar; + } } public void Dispose() diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeSpringBone .cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeSpringBone .cs index 2820b1e49..e0b37334f 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeSpringBone .cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeSpringBone .cs @@ -32,8 +32,7 @@ namespace UniVRM10 { m_instance = instance; - var gltfInstance = instance.GetComponent(); - if (gltfInstance != null) + if (instance.TryGetComponent(out var gltfInstance)) { // ランタイムインポートならここに到達してゼロコストになる m_defaultTransformStates = gltfInstance.InitialTransformStates; diff --git a/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs b/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs index 4cbb3b861..39bce21b7 100644 --- a/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs +++ b/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using UniGLTF; using UniHumanoid; using UnityEngine; @@ -180,7 +181,7 @@ namespace UniVRM10 ControlRig = (provider, provider); // create SkinnedMesh for bone visualize - var animator = GetComponent(); + var animator = this.GetComponentOrThrow(); BoxMan = SkeletonMeshUtility.CreateRenderer(animator); var material = new Material(Shader.Find("Standard")); BoxMan.sharedMaterial = material; diff --git a/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10PoseLoader.cs b/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10PoseLoader.cs index 141a6d892..aa9883aa3 100644 --- a/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10PoseLoader.cs +++ b/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10PoseLoader.cs @@ -151,7 +151,7 @@ namespace UniVRM10 { var (hips, map) = GetPose(humanoid); - var animator = instance.GetComponent(); + var animator = instance.GetComponentOrThrow(); // update src ControlRig animator.GetBoneTransform(HumanBodyBones.Hips).localPosition = hips; @@ -228,7 +228,7 @@ namespace UniVRM10 ); using var loader = new VrmAnimationImporter(data); var gltfInstance = await loader.LoadAsync(new ImmediateCaller()); - var instance = gltfInstance.GetComponent(); + var instance = gltfInstance.GetComponentOrThrow(); if (data.GLTF.extensions is UniGLTF.glTFExtensionImport extensions) { diff --git a/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs b/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs index d35307261..d8f6955ba 100644 --- a/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs +++ b/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs @@ -32,8 +32,8 @@ namespace UniVRM10 // humanoid { - var humanoid = root.GetComponent(); - if (humanoid is null) + var humanoid = root.GetComponentOrNull(); + if (humanoid == null) { humanoid = root.AddComponent(); humanoid.AssignBonesFromAnimator(); @@ -150,8 +150,7 @@ namespace UniVRM10 } else if (renderer is MeshRenderer meshRenderer) { - var filter = meshRenderer.gameObject.GetComponent(); - if (filter != null && MeshCanExport(filter.sharedMesh)) + if (meshRenderer.gameObject.TryGetComponent(out var filter) && MeshCanExport(filter.sharedMesh)) { var mesh = CreateMesh(arrayManager, filter.sharedMesh, meshRenderer, Materials); Model.MeshGroups.Add(mesh); diff --git a/Assets/VRM10/Runtime/IO/Vrm10.cs b/Assets/VRM10/Runtime/IO/Vrm10.cs index 9eeaf41a1..7532f88e3 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10.cs @@ -354,7 +354,7 @@ namespace UniVRM10 throw new Exception("Failed to load by unknown reason."); } - var vrm10Instance = gltfInstance.GetComponent(); + var vrm10Instance = gltfInstance.GetComponentOrNull(); if (vrm10Instance == null) { gltfInstance.Dispose(); diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index f29306031..138a80a90 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -255,7 +255,7 @@ namespace UniVRM10 int? thumbnailIndex) ExportVrm(GameObject root, Model model, ModelExporter converter, VRM10ObjectMeta vrmMeta, List nodes, ITextureExporter textureExporter) { - var vrmController = root?.GetComponent(); + var vrmController = root?.GetComponentOrThrow(); if (vrmMeta == null) { diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index c8638c52b..6fb7d0a1f 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -658,8 +658,7 @@ namespace UniVRM10 // node is required var go = Nodes[gltfJoint.Node.Value].gameObject; - var joint = go.GetComponent(); - if (joint != null) + if (go.TryGetComponent(out var joint)) { // 仕様違反。マイグレーションで発生しうるのと、エクスポーターでの除外などがされていないので、 // エラーにせずに飛ばす diff --git a/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs b/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs index 5eea64aac..933a3727e 100644 --- a/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs +++ b/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs @@ -264,7 +264,7 @@ namespace UniVRM10 if (expressions.Length > 0) { - var animation = instance.GetComponent(); + var animation = instance.GetComponentOrThrow(); var clip = animation.clip; // Expression の float カーブを追加する diff --git a/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs b/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs index 6e8e59cf5..f61c93e6f 100644 --- a/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs +++ b/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs @@ -105,11 +105,7 @@ namespace UniVRM10 public override (List, List) Process( GameObject target, IEnumerable groupCopy) { - _vrmInstance = target.GetComponent(); - if (_vrmInstance == null) - { - throw new ArgumentException(); - } + _vrmInstance = target.GetComponentOrThrow(); // TODO: update: spring // TODO: update: constraint @@ -118,9 +114,12 @@ namespace UniVRM10 if (FreezeBlendShapeRotationAndScaling) { - var animator = target.GetComponent(); - var newAvatar = AvatarDescription.RecreateAvatar(animator); - GameObject.DestroyImmediate(animator); + Avatar newAvatar = default; + if (target.TryGetComponent(out var animator)) + { + newAvatar = AvatarDescription.RecreateAvatar(animator); + GameObject.DestroyImmediate(animator); + } animator = target.AddComponent(); animator.avatar = newAvatar; } @@ -135,7 +134,7 @@ namespace UniVRM10 { return; } - var vrm1 = root.GetComponent(); + var vrm1 = root.GetComponentOrNull(); if (vrm1 == null) { return; diff --git a/Assets/VRM10/Runtime/UnityExtensions.cs b/Assets/VRM10/Runtime/UnityExtensions.cs index b2c7dc1ee..99063dee8 100644 --- a/Assets/VRM10/Runtime/UnityExtensions.cs +++ b/Assets/VRM10/Runtime/UnityExtensions.cs @@ -1,10 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using UnityEngine; -#if UNITY_EDITOR -using UnityEditor; -#endif namespace UniVRM10 @@ -221,14 +217,6 @@ namespace UniVRM10 return current; } - public static IEnumerable GetChildren(this Transform self) - { - foreach (Transform child in self) - { - yield return child; - } - } - public static IEnumerable Traverse(this Transform t) { yield return t; @@ -241,11 +229,6 @@ namespace UniVRM10 } } - public static Transform FindDescenedant(this Transform t, string name) - { - return t.Traverse().First(x => x.name == name); - } - public static IEnumerable Ancestors(this Transform t) { yield return t; @@ -257,74 +240,5 @@ namespace UniVRM10 } } } - - public static float[] ToArray(this Quaternion q) - { - return new float[] { q.x, q.y, q.z, q.w }; - } - - public static float[] ToArray(this Vector3 v) - { - return new float[] { v.x, v.y, v.z }; - } - - public static float[] ToArray(this Vector4 v) - { - return new float[] { v.x, v.y, v.z, v.w }; - } - - public static void ReverseZRecursive(this Transform root) - { - var globalMap = root.Traverse().ToDictionary(x => x, x => PosRot.FromGlobalTransform(x)); - - foreach (var x in root.Traverse()) - { - x.position = globalMap[x].Position.ReverseZ(); - x.rotation = globalMap[x].Rotation.ReverseZ(); - } - } - - public static Mesh GetSharedMesh(this Transform t) - { - var meshFilter = t.GetComponent(); - if (meshFilter != null) - { - return meshFilter.sharedMesh; - } - - var skinnedMeshRenderer = t.GetComponent(); - if (skinnedMeshRenderer != null) - { - return skinnedMeshRenderer.sharedMesh; - } - - return null; - } - - public static Material[] GetSharedMaterials(this Transform t) - { - var renderer = t.GetComponent(); - if (renderer != null) - { - return renderer.sharedMaterials; - } - - return new Material[] { }; - } - - public static bool Has(this Transform transform, T t) where T : Component - { - return transform.GetComponent() == t; - } - - public static T GetOrAddComponent(this GameObject go) where T : Component - { - var c = go.GetComponent(); - if (c != null) - { - return c; - } - return go.AddComponent(); - } } } diff --git a/Assets/VRM10/Samples~/SimpleVrma/SimpleVrma.cs b/Assets/VRM10/Samples~/SimpleVrma/SimpleVrma.cs index dc125a9b8..5db457913 100644 --- a/Assets/VRM10/Samples~/SimpleVrma/SimpleVrma.cs +++ b/Assets/VRM10/Samples~/SimpleVrma/SimpleVrma.cs @@ -47,7 +47,7 @@ public class SimpleVrma : MonoBehaviour showMeshes: false, awaitCaller: new ImmediateCaller()); - var instance = Vrm.GetComponent(); + var instance = Vrm.GetComponentOrThrow(); instance.ShowMeshes(); } @@ -58,11 +58,11 @@ public class SimpleVrma : MonoBehaviour using var loader = new VrmAnimationImporter(data); var instance = await loader.LoadAsync(new ImmediateCaller()); - Vrma = instance.GetComponent(); + Vrma = instance.GetComponentOrThrow(); Vrm.Runtime.VrmAnimation = Vrma; Debug.Log(Vrma); - var animation = Vrma.GetComponent(); + var animation = Vrma.GetComponentOrThrow(); animation.Play(); } } diff --git a/Assets/VRM10/Samples~/URPSample/Runtime/UrpSampleUI.cs b/Assets/VRM10/Samples~/URPSample/Runtime/UrpSampleUI.cs index b952b4786..47d7a08f3 100644 --- a/Assets/VRM10/Samples~/URPSample/Runtime/UrpSampleUI.cs +++ b/Assets/VRM10/Samples~/URPSample/Runtime/UrpSampleUI.cs @@ -46,7 +46,7 @@ namespace UniVRM10.URPSample return; } - var instance = _loadedVrm.GetComponent(); + var instance = _loadedVrm.GetComponentOrThrow(); instance.ShowMeshes(); instance.EnableUpdateWhenOffscreen(); diff --git a/Assets/VRM10/Samples~/VRM10FirstPersonSample/VRM10RuntimeLoader.cs b/Assets/VRM10/Samples~/VRM10FirstPersonSample/VRM10RuntimeLoader.cs index f5b9328fc..2ecba4ef7 100644 --- a/Assets/VRM10/Samples~/VRM10FirstPersonSample/VRM10RuntimeLoader.cs +++ b/Assets/VRM10/Samples~/VRM10FirstPersonSample/VRM10RuntimeLoader.cs @@ -33,8 +33,7 @@ namespace UniVRM10.FirstPersonSample m_target.Source = m_source; m_target.SourceType = UniHumanoid.HumanPoseTransfer.HumanPoseTransferSourceType.HumanPoseTransfer; - var animator = m_target.GetComponent(); - if (animator != null) + if (m_target.TryGetComponent(out var animator)) { if (m_faceCamera != null) { @@ -91,7 +90,7 @@ namespace UniVRM10.FirstPersonSample // VR用 FirstPerson 設定 await instance.Vrm.FirstPerson.SetupAsync(instance.gameObject, awaitCaller); - instance.GetComponent().ShowMeshes(); + instance.GetComponentOrThrow().ShowMeshes(); return instance; } @@ -127,7 +126,7 @@ namespace UniVRM10.FirstPersonSample { GameObject.Destroy(m_source.gameObject); } - m_source = context.Root.GetComponent(); + m_source = context.Root.GetComponentOrThrow(); SetupTarget(m_target); } diff --git a/Assets/VRM10/Samples~/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs b/Assets/VRM10/Samples~/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs index 679e49ede..b99ce3bf9 100644 --- a/Assets/VRM10/Samples~/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs +++ b/Assets/VRM10/Samples~/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs @@ -47,7 +47,7 @@ namespace UniVRM10.RuntimeExporterSample } var vrm10 = await Vrm10.LoadPathAsync(path); - var loaded = vrm10.GetComponent(); + var loaded = vrm10.GetComponentOrThrow(); loaded.ShowMeshes(); loaded.EnableUpdateWhenOffscreen();