导入角色动画,和增加角色控制

This commit is contained in:
2025-12-11 19:30:20 +08:00
parent a60a92e7ba
commit 7775fa30bb
1452 changed files with 592217 additions and 42573 deletions

View File

@@ -0,0 +1,20 @@
using System;
namespace KINEMATION.KAnimationCore.Runtime.Input
{
[Obsolete("use `UserInputController` instead.")]
public interface IUserInputController
{
public void Initialize();
public int GetPropertyIndex(string propertyName);
public void SetValue(string propertyName, object value);
public T GetValue<T>(string propertyName);
public void SetValue(int propertyIndex, object value);
public T GetValue<T>(int propertyIndex);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ca9ff9d900444c4785ba2de65c0e0263
timeCreated: 1707563078

View File

@@ -0,0 +1,34 @@
using System;
using UnityEngine;
namespace KINEMATION.KAnimationCore.Runtime.Input
{
[Serializable]
public struct BoolProperty
{
public string name;
public bool defaultValue;
}
[Serializable]
public struct IntProperty
{
public string name;
public int defaultValue;
}
[Serializable]
public struct FloatProperty
{
public string name;
public float defaultValue;
public float interpolationSpeed;
}
[Serializable]
public struct VectorProperty
{
public string name;
public Vector4 defaultValue;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 37f35ec97fc94416a139ca21ae59beda
timeCreated: 1707463958

View File

@@ -0,0 +1,16 @@
// Designed by KINEMATION, 2024.
using System.Collections.Generic;
using UnityEngine;
namespace KINEMATION.KAnimationCore.Runtime.Input
{
[CreateAssetMenu(fileName = "NewInputConfig", menuName = "KINEMATION/Input Config")]
public class UserInputConfig : ScriptableObject
{
public List<IntProperty> intProperties = new List<IntProperty>();
public List<FloatProperty> floatProperties = new List<FloatProperty>();
public List<BoolProperty> boolProperties = new List<BoolProperty>();
public List<VectorProperty> vectorProperties = new List<VectorProperty>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0e8993601b7c484b862c9c567134ed2e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: a497183ade831dc4aa44bf44b5ce27b8, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,194 @@
// Designed by KINEMATION, 2024.
using KINEMATION.KAnimationCore.Runtime.Core;
using System.Collections.Generic;
using UnityEngine;
namespace KINEMATION.KAnimationCore.Runtime.Input
{
public class UserInputController : MonoBehaviour
{
[SerializeField] public UserInputConfig inputConfig;
protected List<object> _inputProperties;
protected Dictionary<string, int> _inputPropertyMap;
protected (int, float, float)[] _floatsToInterpolate;
public UserInputConfig GetConfig()
{
return inputConfig;
}
protected virtual void Update()
{
if (_floatsToInterpolate == null) return;
foreach (var tuple in _floatsToInterpolate)
{
float value = (float) _inputProperties[tuple.Item1];
if (Mathf.Approximately(value, tuple.Item3))
{
value = tuple.Item3;
}
else
{
float alpha = KMath.ExpDecayAlpha(Time.deltaTime, tuple.Item2);
value = Mathf.LerpUnclamped(value, tuple.Item3, alpha);
}
_inputProperties[tuple.Item1] = value;
}
}
public virtual void Initialize()
{
#if UNITY_EDITOR
_propertyNames = new List<(string, object)>();
#endif
_inputProperties = new List<object>();
_inputPropertyMap = new Dictionary<string, int>();
List<(int, float, float)> floatsToInterpolate = new List<(int, float, float)>();
int index = 0;
foreach (var property in inputConfig.boolProperties)
{
_inputProperties.Add(property.defaultValue);
_inputPropertyMap.TryAdd(property.name, index);
index++;
#if UNITY_EDITOR
_propertyNames.Add((property.name, null));
#endif
}
foreach (var property in inputConfig.intProperties)
{
_inputProperties.Add(property.defaultValue);
_inputPropertyMap.TryAdd(property.name, index);
index++;
#if UNITY_EDITOR
_propertyNames.Add((property.name, null));
#endif
}
foreach (var property in inputConfig.floatProperties)
{
_inputProperties.Add(property.defaultValue);
_inputPropertyMap.TryAdd(property.name, index);
if (!Mathf.Approximately(property.interpolationSpeed, 0f))
{
floatsToInterpolate.Add((index, property.interpolationSpeed, property.defaultValue));
}
index++;
#if UNITY_EDITOR
_propertyNames.Add((property.name, null));
#endif
}
if (floatsToInterpolate.Count > 0)
{
_floatsToInterpolate = floatsToInterpolate.ToArray();
}
foreach (var property in inputConfig.vectorProperties)
{
_inputProperties.Add(property.defaultValue);
_inputPropertyMap.TryAdd(property.name, index);
index++;
#if UNITY_EDITOR
_propertyNames.Add((property.name, null));
#endif
}
}
public int GetPropertyIndex(string propertyName)
{
if (_inputPropertyMap.TryGetValue(propertyName, out int index))
{
return index;
}
return -1;
}
public virtual void SetValue(string propertyName, object value)
{
SetValue(GetPropertyIndex(propertyName), value);
}
public virtual T GetValue<T>(string propertyName)
{
return GetValue<T>(GetPropertyIndex(propertyName));
}
public virtual void SetValue(int propertyIndex, object value)
{
if (propertyIndex < 0 || propertyIndex > _inputProperties.Count - 1)
{
return;
}
if (_floatsToInterpolate != null)
{
int floatToInterpolateIndex = -1;
for (int i = 0; i < _floatsToInterpolate.Length; i++)
{
if (_floatsToInterpolate[i].Item1 == propertyIndex)
{
floatToInterpolateIndex = i;
}
}
if (floatToInterpolateIndex != -1)
{
var tuple = _floatsToInterpolate[floatToInterpolateIndex];
tuple.Item3 = (float) value;
_floatsToInterpolate[floatToInterpolateIndex] = tuple;
return;
}
}
_inputProperties[propertyIndex] = value;
}
public virtual T GetValue<T>(int propertyIndex)
{
if (propertyIndex < 0 || propertyIndex > _inputProperties.Count - 1)
{
return default(T);
}
return (T) _inputProperties[propertyIndex];
}
#if UNITY_EDITOR
protected List<(string, object)> _propertyNames;
public virtual (string, object)[] GetPropertyBindings()
{
if (_propertyNames == null) return null;
int count = _propertyNames.Count;
for (int i = 0; i < count; i++)
{
var item = _propertyNames[i];
item.Item2 = _inputProperties[i];
_propertyNames[i] = item;
}
return _propertyNames.ToArray();
}
#endif
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 26b2896c936d4be59e98d9bf358c5a96
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 6fd4d99d3a1edc7408fe599214be6059, type: 3}
userData:
assetBundleName:
assetBundleVariant: