阶段性完成

This commit is contained in:
SoulliesOfficial
2025-12-08 05:27:53 -05:00
parent ef7b479712
commit f7af60351b
8770 changed files with 15637030 additions and 208354 deletions

View File

@@ -0,0 +1,316 @@
using System.Collections.Generic;
using UnityEngine;
namespace FIMSpace.Basics
{
/// <summary>
/// FM: Basic class for holding animation clips as hashes inside dictionary and with some other handy methods
/// Add your animation clips with fAnimationClip.AddClip(animator, Idle) then use it by fAnimationClip["Idle"]
/// </summary>
public class FAnimationClips : Dictionary<string, int>
{
/// <summary> Reference to Unity's Animator component </summary>
public readonly Animator Animator;
/// <summary> Lastest crossfaded from code animation clip name </summary>
public string CurrentAnimation { get; private set; }
/// <summary> Previously crossfaded from code animation clip name </summary>
public string PreviousAnimation { get; private set; }
public int Layer = 0;
/// <summary>
/// ADD ANIMATION CLIPS USING - AddClip() - method !!!
/// </summary>
public FAnimationClips(Animator animator)
{
this.Animator = animator;
CurrentAnimation = "";
PreviousAnimation = "";
}
/// <summary>
/// Checking if inside animator clipName state exist and with 'exactClipName' false it will check all variants of clipName, uppercase / lowercase etc.
/// </summary>
public void Add(string clipName, bool exactClipName = false)
{
AddClip(clipName, exactClipName);
}
/// <summary>
/// Checking if inside animator clipName state exist and with 'exactClipName' false it will check all variants of clipName, uppercase / lowercase etc.
/// </summary>
public void AddClip(string clipName, bool exactClipName = false)
{
AddClip(Animator, clipName, exactClipName);
}
/// <summary>
/// Checking if inside animator clipName state exist and with 'exactClipName' false it will check all variants of clipName, uppercase / lowercase etc.
/// </summary>
public void AddClip(Animator animator, string clipName, bool exactClipName = false)
{
if (!animator)
{
Debug.LogError("No animator!");
return;
}
string existing = "";
if (!exactClipName) // Checking if animation state exists with different variants for clipName word
{
if (FAnimatorMethods.StateExists(animator, clipName, Layer)) existing = clipName;
else
if (FAnimatorMethods.StateExists(animator, FStringMethods.CapitalizeFirstLetter(clipName))) existing = FStringMethods.CapitalizeFirstLetter(clipName);
else
if (FAnimatorMethods.StateExists(animator, clipName.ToLower(), Layer)) existing = clipName.ToLower();
else
if (FAnimatorMethods.StateExists(animator, clipName.ToUpper(), Layer)) existing = clipName.ToUpper();
}
else // Checking if state with provided exact name exists inside animator
{
if (FAnimatorMethods.StateExists(animator, clipName, Layer)) existing = clipName;
}
if (existing == "")
{
Debug.LogWarning("Clip with name " + clipName + " not exists in animator from game object " + animator.gameObject.name);
}
else // Adding clip hash to dictionary if it exists inside animator
{
if (!ContainsKey(clipName))
Add(clipName, Animator.StringToHash(existing));
}
}
/// <summary>
/// Transitioning to choosed animation by dictionary
/// </summary>
public void CrossFadeInFixedTime(string clip, float transitionTime = 0.25f, float timeOffset = 0f, bool startOver = false)
{
if (this.ContainsKey(clip))
{
RefreshClipMemory(clip);
if (startOver)
Animator.CrossFadeInFixedTime(this[clip], transitionTime, Layer, timeOffset);
else
if (!IsPlaying(clip))
Animator.CrossFadeInFixedTime(this[clip], transitionTime, Layer, timeOffset);
}
}
/// <summary>
/// Transitioning to choosed animation by dictionary
/// </summary>
public void CrossFade(string clip, float transitionTime = 0.25f, float timeOffset = 0f, bool startOver = false)
{
if (this.ContainsKey(clip))
{
RefreshClipMemory(clip);
if (startOver)
Animator.CrossFade(this[clip], transitionTime, Layer, timeOffset);
else
if (!IsPlaying(clip))
Animator.CrossFade(this[clip], transitionTime, Layer, timeOffset);
}
}
/// <summary>
/// Storing lately and currently played clip in variables
/// </summary>
private void RefreshClipMemory(string name)
{
if (name != CurrentAnimation)
{
PreviousAnimation = CurrentAnimation;
CurrentAnimation = name;
}
}
/// <summary>
/// Changing float parameter value smoothly (when speed value about 10, 60 is instant)
/// </summary>
public void SetFloat(string parameter, float value = 0f, float deltaSpeed = 60f)
{
float newValue = Animator.GetFloat(parameter);
if (deltaSpeed >= 60f) newValue = value; else newValue = FLogicMethods.FLerp(newValue, value, Time.deltaTime * deltaSpeed);
Animator.SetFloat(parameter, newValue);
}
/// <summary>
/// Changing float parameter value smoothly (when speed value about 10, 60 is instant)
/// </summary>
public void SetFloatUnscaledDelta(string parameter, float value = 0f, float deltaSpeed = 60f)
{
float newValue = Animator.GetFloat(parameter);
if (deltaSpeed >= 60f) newValue = value; else newValue = FLogicMethods.FLerp(newValue, value, Time.unscaledDeltaTime * deltaSpeed);
Animator.SetFloat(parameter, newValue);
}
/// <summary>
/// If animator is truly playing clip with given string id (added by you using clips.AddClip(name) )
/// </summary>
internal bool IsPlaying(string clip)
{
AnimatorStateInfo info;
if (Animator.IsInTransition(Layer))
{
info = Animator.GetNextAnimatorStateInfo(Layer);
if (info.shortNameHash == this[clip]) return true;
}
else
{
info = Animator.GetCurrentAnimatorStateInfo(Layer);
if (info.shortNameHash == this[clip]) return true;
}
return false;
}
}
public class FAnimator
{
/// <summary> Reference to Unity's Animator component </summary>
public readonly Animator Animator;
/// <summary> Lastest crossfaded from code animation clip name </summary>
public string CurrentAnimation { get; private set; }
/// <summary> Previously crossfaded from code animation clip name </summary>
public string PreviousAnimation { get; private set; }
public int Layer { get; private set; }
/// <summary>
/// ADD ANIMATION CLIPS USING - AddClip() - method !!!
/// </summary>
public FAnimator(Animator animator, int layer = 0)
{
this.Animator = animator;
CurrentAnimation = "";
PreviousAnimation = "";
Layer = layer;
}
/// <summary>
/// Checking if inside animator clipName state exist and with 'exactClipName' false it will check all variants of clipName, uppercase / lowercase etc.
/// </summary>
public bool ContainsClip(string clipName, bool exactClipName = false)
{
if (!Animator)
{
Debug.LogError("No animator!");
return false;
}
string existing = "";
if (!exactClipName) // Checking if animation state exists with different variants for clipName word
{
if (FAnimatorMethods.StateExists(Animator, clipName, Layer)) existing = clipName;
else
if (FAnimatorMethods.StateExists(Animator, FStringMethods.CapitalizeFirstLetter(clipName))) existing = FStringMethods.CapitalizeFirstLetter(clipName);
else
if (FAnimatorMethods.StateExists(Animator, clipName.ToLower(), Layer)) existing = clipName.ToLower();
else
if (FAnimatorMethods.StateExists(Animator, clipName.ToUpper(), Layer)) existing = clipName.ToUpper();
}
else // Checking if state with provided exact name exists inside animator
{
if (FAnimatorMethods.StateExists(Animator, clipName, Layer)) existing = clipName;
}
if (existing == "")
{
Debug.LogWarning("Clip with name " + clipName + " not exists in animator from game object " + Animator.gameObject.name);
return false;
}
else
return true;
}
/// <summary>
/// Transitioning to choosed animation by dictionary
/// </summary>
public void CrossFadeInFixedTime(string clip, float transitionTime = 0.25f, float timeOffset = 0f, bool startOver = false)
{
RefreshClipMemory(clip);
if (startOver)
Animator.CrossFadeInFixedTime(clip, transitionTime, Layer, timeOffset);
else
if (!IsPlaying(clip))
Animator.CrossFadeInFixedTime(clip, transitionTime, Layer, timeOffset);
}
/// <summary>
/// Transitioning to choosed animation by dictionary
/// </summary>
public void CrossFade(string clip, float transitionTime = 0.25f, float timeOffset = 0f, bool startOver = false)
{
RefreshClipMemory(clip);
if (startOver)
Animator.CrossFade(clip, transitionTime, Layer, timeOffset);
else
if (!IsPlaying(clip))
Animator.CrossFade(clip, transitionTime, Layer, timeOffset);
}
/// <summary>
/// Storing lately and currently played clip in variables
/// </summary>
private void RefreshClipMemory(string name)
{
if (name != CurrentAnimation)
{
PreviousAnimation = CurrentAnimation;
CurrentAnimation = name;
}
}
/// <summary>
/// Changing float parameter value smoothly (when speed value about 10, 60 is instant)
/// </summary>
public void SetFloat(string parameter, float value = 0f, float deltaSpeed = 60f)
{
float newValue = Animator.GetFloat(parameter);
if (deltaSpeed >= 60f) newValue = value; else newValue = FLogicMethods.FLerp(newValue, value, Time.deltaTime * deltaSpeed);
Animator.SetFloat(parameter, newValue);
}
/// <summary>
/// Changing float parameter value smoothly (when speed value about 10, 60 is instant)
/// </summary>
public void SetFloatUnscaledDelta(string parameter, float value = 0f, float deltaSpeed = 60f)
{
float newValue = Animator.GetFloat(parameter);
if (deltaSpeed >= 60f) newValue = value; else newValue = FLogicMethods.FLerp(newValue, value, Time.unscaledDeltaTime * deltaSpeed);
Animator.SetFloat(parameter, newValue);
}
/// <summary>
/// If animator is truly playing clip with given string id (added by you using clips.AddClip(name) )
/// </summary>
internal bool IsPlaying(string clip)
{
AnimatorStateInfo info;
if (Animator.IsInTransition(Layer))
{
info = Animator.GetNextAnimatorStateInfo(Layer);
if (info.shortNameHash == Animator.StringToHash(clip) ) return true;
}
else
{
info = Animator.GetCurrentAnimatorStateInfo(Layer);
if (info.shortNameHash == Animator.StringToHash(clip)) return true;
}
return false;
}
}
}

View File

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

View File

@@ -0,0 +1,91 @@
using UnityEngine;
namespace FIMSpace
{
/// <summary>
/// FMoeglich: Class with methods which can be helpful when using unity Animator class
/// </summary>
public static class FAnimatorMethods
{
/// <summary>
/// Sets animator's float value with lerp
/// </summary>
public static void LerpFloatValue(this Animator animator, string name = "RunWalk", float value = 0f, float deltaSpeed = 8f)
{
float newValue = animator.GetFloat(name);
newValue = Mathf.Lerp(newValue, value, Time.deltaTime * deltaSpeed);
animator.SetFloat(name, newValue);
}
/// <summary>
/// Function called to detect if no-looped animation finish
/// </summary>
public static bool CheckAnimationEnd(this Animator animator, int layer = 0, bool reverse = false, bool checkAnimLoop = true)
{
AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layer);
if (!animator.IsInTransition(layer))
{
if (checkAnimLoop)
{
if (info.loop == false)
{
if (!reverse)
if (info.normalizedTime > 0.98f) return true;
else
if (info.normalizedTime < 0.02f) return true;
}
}
else /* Same operation as above but without checking if animation is looped in the source */
{
if (!reverse)
if (info.normalizedTime > 0.98f) return true;
else
if (info.normalizedTime < 0.02f) return true;
}
}
return false;
}
/// <summary>
/// Resetting all additional layers' weights to zero (lerp but reaching value)
/// </summary>
public static void ResetLayersWeights(this Animator animator, float speed = 10f)
{
for (int i = 1; i < animator.layerCount; i++)
{
animator.SetLayerWeight(i, animator.GetLayerWeight(i).Lerp(0f, Time.deltaTime * speed));
}
}
/// <summary>
/// Transitioning value of animator layer's weight to target with smooth effect
/// </summary>
public static void LerpLayerWeight(this Animator animator, int layer = 0, float newValue = 1f, float speed = 8f)
{
float newWeight = animator.GetLayerWeight(layer);
newWeight.Lerp(newValue, Time.deltaTime * speed);
if (newValue == 1f) if (newWeight > 0.999f) newWeight = 1f;
if (newValue == 0f) if (newWeight < 0.01f) newWeight = 0f;
animator.SetLayerWeight(layer, newWeight);
}
/// <summary>
/// Returning true if state exist
/// </summary>
public static bool StateExists(this Animator animator, string clipName, int layer = 0)
{
int animHash = Animator.StringToHash(clipName);
return animator.HasState(layer, animHash);
}
}
}

View File

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