key | 标识设置的键。 |
T 具有分配了相应远程值的字段的对象的实例。
获取与 **key** 标识的远程设置对应的对象(如果存在)。
Remote Settings 构造一个类型为 T
的对象,并将其字段或属性设置为相应的远程值,从而将字段名与键名相匹配。该过程忽略对象中与远程值不对应的字段,同样,还会忽略与类型中的字段或属性不对应的远程值。
如果在调用 GetObject()
时没有指定键,则 Remote Settings 将所有远程设置视为一个对象。如果指定的键不存在,则此函数返回 null。
Remote Settings 会转换数字和布尔值类型,但不转换字符串类型。例如,如果将浮点设置映射到整数字段,则浮点值将转换为整数。但如果尝试将数字或布尔值设置映射到字符串字段,则该字符串字段将保留为 null。如果远程设置包含对象,则根据父对象结构或类中的字段的类型转换该对象。
using UnityEngine;
public class HandleRemoteSettingsGetObject : MonoBehaviour { [System.Serializable] public struct MySettings { public bool enableBoss; public int maxLevelDifficulty; public string defaultPlayerName; public float gameBaseAcceleration; }
private void Start() { MySettings ms = RemoteSettings.GetObject<MySettings>("myGameSettings"); Debug.Log(ms.maxLevelDifficulty); } }
key | 标识设置的键。 |
type | RemoteSettings 中表示的对象的类型。 |
object 具有分配了相应远程值的字段的对象的实例。
获取与 **key** 标识的远程设置对应的对象(如果存在)。
Remote Settings 构造一个类型由 type
参数指定的对象,并将其字段或属性设置为相应的远程值,从而使字段名与键名相匹配。该过程忽略对象中与远程值不对应的字段,同样,还会忽略与类型中的字段或属性不对应的远程值。
如果在调用 GetObject()
时没有指定键,则 Remote Settings 将所有远程设置视为一个对象。如果指定的键不存在,则此函数返回 null。
Remote Settings 会转换数字和布尔值类型,但不转换字符串类型。例如,如果将浮点设置映射到整数字段,则浮点值将转换为整数。但如果尝试将数字或布尔值设置映射到字符串字段,则该字符串字段将保留为 null。如果远程设置包含对象,则根据父对象结构或类中的字段的类型转换该对象。
using UnityEngine;
public class HandleRemoteSettingsGetObjectWithType : MonoBehaviour { [System.Serializable] public struct MyCustomSettings { public bool enableBoss; public int maxLevelDifficulty; public string defaultPlayerName; public float gameBaseAcceleration; }
private void Start() { MyCustomSettings ms = (MyCustomSettings)RemoteSettings.GetObject(typeof(MyCustomSettings), "myGameSettings"); Debug.Log(ms.maxLevelDifficulty); } }
defaultValue | 用于默认值的对象。 |
key | 标识设置的键。 |
object 具有分配了相应远程值的字段的对象的实例。
获取与 **key** 标识的远程设置对应的对象(如果存在)。
Remote Settings 构造一个类型由 type
参数指定的对象,并将其字段或属性设置为相应的远程值,从而使字段名与键名相匹配。该过程忽略对象中与远程值不对应的字段,同样,还会忽略与类型中的字段或属性不对应的远程值。
如果在调用 GetObject()
时没有指定键,则 Remote Settings 将所有远程设置视为一个对象。如果指定的键不存在,则此函数返回 null。
Remote Settings 会转换数字和布尔值类型,但不转换字符串类型。例如,如果将浮点设置映射到整数字段,则浮点值将转换为整数。但如果尝试将数字或布尔值设置映射到字符串字段,则该字符串字段将保留为 null。如果远程设置包含对象,则根据父对象结构或类中的字段的类型转换该对象。
using UnityEngine;
public class HandleRemoteSettingsGetObjectWithDefault : MonoBehaviour { [System.Serializable] public struct MySettingValues { public bool enableBoss; public int maxLevelDifficulty; public string defaultPlayerName; public float gameBaseAcceleration; }
private void Start() { MySettingValues defaultValue = new MySettingValues(); defaultValue.enableBoss = true; MySettingValues ms = (MySettingValues)RemoteSettings.GetObject("myGameSettings", defaultValue); Debug.Log(ms.maxLevelDifficulty); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.