This commit is contained in:
CortexCore
2023-11-06 01:17:23 +08:00
parent bd40165ade
commit 5446067f91
114 changed files with 2023 additions and 414 deletions

View File

@@ -0,0 +1,56 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Assertions;
namespace BITKit
{
public static class ConstraintExtensions
{
public static ParentConstraint SetParentConstraint(this Transform target, Transform parentSource,
ParentConstraint constraint = null)
{
Assert.IsTrue(!constraint || target == constraint.transform);
if (!constraint)
{
if (!parentSource) return null;
constraint = target.gameObject.GetOrAddComponent<ParentConstraint>();
}
// 清空已有约束
constraint.constraintActive = false;
for (int i = constraint.sourceCount - 1; i >= 0; i--)
{
constraint.RemoveSource(i);
}
// 无约束
if (!parentSource) return constraint;
// 设置新约束
constraint.AddSource(new ConstraintSource
{
sourceTransform = parentSource,
weight = 1,
});
// 设置 Position offset
var positionOffset = parentSource.InverseTransformPoint(target.position);
constraint.SetTranslationOffset(0, positionOffset);
// 设置 Rotation offset
var localForward = parentSource.InverseTransformDirection(target.forward);
var localUpward = parentSource.InverseTransformDirection(target.up);
var rotationOffset = Quaternion.LookRotation(localForward, localUpward).eulerAngles;
constraint.SetRotationOffset(0, rotationOffset);
// 激活约束
constraint.constraintActive = true;
return constraint;
}
}
}

View File

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

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit
{
public static class GameObjectExtensions
{
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
{
return gameObject.TryGetComponent<T>(out var t) ? t : gameObject.AddComponent<T>();
}
public static T RemoveComponent<T>(this GameObject gameObject) where T : Component
{
var t = gameObject.GetComponent<T>();
if (t is null) return null;
Object.Destroy(t);
return t;
}
}
}

View File

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