如何使用字典使C#for unity脚本中的代码整洁

如何使用字典使C#for unity脚本中的代码整洁,c#,unity3d,C#,Unity3d,这是我为unity中的animator控制器编写的代码,它工作得很好,但已经表明它只是一遍又一遍地重复我自己来更改一些变量。 我的问题是,是否有办法用C语言编写一本词典,以减少重复性。 我想应该有办法,但由于我是c#的新手,我将非常感谢您的帮助 using System.Collections; using System.Collections.Generic; using UnityEngine; public class anim : MonoBehaviour { [Serial

这是我为unity中的animator控制器编写的代码,它工作得很好,但已经表明它只是一遍又一遍地重复我自己来更改一些变量。 我的问题是,是否有办法用C语言编写一本词典,以减少重复性。 我想应该有办法,但由于我是c#的新手,我将非常感谢您的帮助

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class anim : MonoBehaviour
{
    [SerializeField] private Animator animator = null;


    private static readonly int hashHappy = Animator.StringToHash("happy");
    private static readonly int hashSad = Animator.StringToHash("sad");

    
    void Update()
     {
         if(Input.GetKey(KeyCode.H))
         {

             animator.SetBool(hashHappy, true);
             animator.SetBool(hashSad, false);

         } else if (Input.GetKeyUp(KeyCode.H))
         {
             Invoke("finishHappy", 5f);
           
         }

         if(Input.GetKey(KeyCode.S)){

             animator.SetBool(hashHappy, false);
             animator.SetBool(hashSad, true);

         } else if (Input.GetKeyUp(KeyCode.S))
         {
             Invoke("finishSad", 5f);
           
         }





        

     }

     public void finishHappy()=>animator.SetBool(hashHappy, false);
     public void finishSad()=>animator.SetBool(hashSad, false);

 
        
   


}

公平的警告,我不知道你所做的是否有意义,因为我的统一知识有限

但是,我将使用的数据结构是
列表
。这将允许您对其进行哈希迭代并切换动画

给定的

public static class SomeClass
{
   private static readonly int hashHappy = Animator.StringToHash("happy");
   private static readonly int hashClap = Animator.StringToHash("clap");
   private static readonly int hashSad = Animator.StringToHash("sad");
   private static readonly int hashAngry = Animator.StringToHash("angry");
   private static readonly int hashDance = Animator.StringToHash("dance");
   private static readonly int hashDie = Animator.StringToHash("die");

   privatestatic List<int> _hashes;

   static SomeClass()
   {
      _hashes = new List<int>() {hashHappy, hashClap, hashSad, hashAngry, hashDance, hashDie};
   }

   public static void Set(Animator animator, int hash)
   {
      foreach (var item in _hashes)
         animator.SetBool(hash, item == hash);
   }
}

我可能会像你第一次想到的那样倾向于使用字典。然后将其与一些枚举表达式配对。最终结果如下所示:

public enum ExpressionType
{
    None,
    Happy,
    Sad,
    Angry,
    // etc. etc.
}

public class anim : MonoBehaviour
{
    [SerializeField] private Animator animator = null;

    private static Dictionary<ExpressionType, int> expressions = new Dictionary<ExpressionType, int> ( )
    {
        [ ExpressionType.Happy ] = Animator.StringToHash ( "happy" ),
        [ ExpressionType.Sad ] = Animator.StringToHash ( "sad" ),
        [ ExpressionType.Angry ] = Animator.StringToHash ( "angry" ),
    };

    private static anim _instance;
    private void Awake ( )
    {
        _instance = this;
    }

    public static bool Set ( ExpressionType e = ExpressionType.None )
    {
        if ( _instance ) return false;
        foreach ( var kv in expressions )
            _instance.animator.SetBool ( kv.Value, kv.Key == e );
        return true;
    }

    void Update ( )
    {
        if ( Input.GetKeyDown ( KeyCode.H ) )
            Set ( ExpressionType.Happy );
        else if ( Input.GetKeyUp ( KeyCode.H ) )
            Invoke ( "Set", 5f );

        if ( Input.GetKey ( KeyCode.S ) )
            Set ( ExpressionType.Sad );
        else if ( Input.GetKeyUp ( KeyCode.S ) )
            Invoke ( "Set", 5f );
    }
}
公共枚举表达式类型
{
没有一个
快乐的
悲哀的
发怒的
//等等等等。
}
公共类动画:单一行为
{
[SerializeField]私有Animator Animator=null;
私有静态字典表达式=新字典()
{
[ExpressionType.Happy]=Animator.StringToHash(“Happy”),
[ExpressionType.Sad]=Animator.StringToHash(“Sad”),
[ExpressionType.Angry]=Animator.StringToHash(“Angry”),
};
私有静态动画实例;
私人空间()
{
_实例=此;
}
公共静态布尔集合(ExpressionType e=ExpressionType.None)
{
if(_实例)返回false;
foreach(表达式中的var kv)
_instance.animator.SetBool(千伏值,千伏键==e);
返回true;
}
无效更新()
{
if(Input.GetKeyDown(KeyCode.H))
Set(ExpressionType.Happy);
else if(Input.GetKeyUp(KeyCode.H))
调用(“Set”,5f);
if(Input.GetKey(KeyCode.S))
Set(ExpressionType.Sad);
else if(Input.GetKeyUp(KeyCode.S))
调用(“Set”,5f);
}
}
不向静态
Set
方法传递任何参数将强制将所有bool值设置为false,这就是为什么可以使用调用
Invoke(“Set”,5f)
,而不是尝试精确指定要设置为false的animator Boolean

如果您想保存对动画师的几个不必要的调用,可以添加一个单独的
Reset
方法,但如果您保持原样,它不会破坏任何东西


请注意,只有在使用bool animator值时,这才是真正有益的。设置触发器或传递值需要
Set
执行一些额外的数据类型检查。最后一点是,我已经编写了代码,但还没有测试过。不过看起来它应该可以做到这一点。

谢谢您的解释和代码,它对我来说很好
public enum ExpressionType
{
    None,
    Happy,
    Sad,
    Angry,
    // etc. etc.
}

public class anim : MonoBehaviour
{
    [SerializeField] private Animator animator = null;

    private static Dictionary<ExpressionType, int> expressions = new Dictionary<ExpressionType, int> ( )
    {
        [ ExpressionType.Happy ] = Animator.StringToHash ( "happy" ),
        [ ExpressionType.Sad ] = Animator.StringToHash ( "sad" ),
        [ ExpressionType.Angry ] = Animator.StringToHash ( "angry" ),
    };

    private static anim _instance;
    private void Awake ( )
    {
        _instance = this;
    }

    public static bool Set ( ExpressionType e = ExpressionType.None )
    {
        if ( _instance ) return false;
        foreach ( var kv in expressions )
            _instance.animator.SetBool ( kv.Value, kv.Key == e );
        return true;
    }

    void Update ( )
    {
        if ( Input.GetKeyDown ( KeyCode.H ) )
            Set ( ExpressionType.Happy );
        else if ( Input.GetKeyUp ( KeyCode.H ) )
            Invoke ( "Set", 5f );

        if ( Input.GetKey ( KeyCode.S ) )
            Set ( ExpressionType.Sad );
        else if ( Input.GetKeyUp ( KeyCode.S ) )
            Invoke ( "Set", 5f );
    }
}