RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1294885
Accepted
Rumata
Rumata
Asked:2022-06-14 02:04:27 +0000 UTC2022-06-14 02:04:27 +0000 UTC 2022-06-14 02:04:27 +0000 UTC

如何在不传递所有参数的情况下调用函数?

  • 772

有一个函数最多接受 4 个参数。同时,在不同的情况下,我会调用它并只传递 3、2 或 1 个参数。Unity 会抛出一个错误,如果在函数中设置了参数,则必须传递它。有没有办法将参数指定为可选,或者使用另一种解决方案(哪个)更好?

例子:

void example(Texture texture1, Texture texture2, Texture texture3, Texture texture4) {

  renderer1.material.mainTexture = texture1;

  if (texture2 != null) {
    renderer2.material.mainTexture = texture2;
  }
  if (texture3 != null) {
    renderer3.material.mainTexture = texture3;
  }
  if (texture4 != null) {
    renderer4.material.mainTexture = texture4;
  }

}

public void callExample(){
     example(texture1, texture2);
};
c#
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    Sergey Skvortsov
    2022-06-14T02:32:25Z2022-06-14T02:32:25Z

    参数可以使用默认值: private void example(Texture texture1, Texture texture2 = null, Texture texture3 = null, Texture texture4 = null)

    此外,您可以使用params 关键字: private void example(params Texture[] textures) 现在您可以example使用任意数量的 Texture 类型的参数调用该方法。所有元素将被放置在一个数组中textures

    • 4
  2. Rumata
    2022-06-14T02:30:25Z2022-06-14T02:30:25Z

    就我而言,据我了解,我需要为参数设置默认值,如下所示:

    void example(Texture texture1, Texture texture2 = null, Texture texture3 = null, Texture texture4 = null)
    {
    } 
    
    • -1
  3. pasx
    2022-06-14T17:48:03Z2022-06-14T17:48:03Z

    由于参数个数是固定的,所以使用默认值而不是params——第一个除外。

    还要检查第一个是否为null。

    可以使用局部函数来简化代码。

    您的渲染器是在函数范围之外定义的,因此它们必须是成员变量,因此以“_”开头命名它们,例如:_renderer1。

        private void SetTexture(Texture texture1, Texture texture2 = null, Texture texture3 = null, Texture texture4 = null)
        {
            if (texture1 == null)
                throw new ArgumentNullException(nameof(texture1));
                
            void SetTexture(Renderer renderer, Texture texture) =>
                if (texture != null)
                    renderer.material.mainTexture = texture;
            
            SetTexture(_renderer1, texture1);
            SetTexture(_renderer2, texture2);
            SetTexture(_renderer3, texture3);
            SetTexture(_renderer4, texture4);
        }
    

    您还可以在渲染器中使用扩展方法,如下所示:

        public static void class RendererExtensions
        {
            public static void SetTexture(this Renderer renderer, Texture texture = null) =>
                if (texture != null)
                    renderer.material.mainTexture = texture;
        }
    
    • -1

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5