如何使用代码更新统一资产文件

时间:2019-02-20 03:44:32

标签: unity3d unityscript

我想通过GUI操作在调试和发布之间切换环境。

一些第三方SDK配置需要在.asset文件中更改。我使用 AssetDatabase

[MenuItem("TEST/Switch Environment/Switch to Dev")]
public static void Switch2Dev()
{

    object []data = AssetDatabase.LoadAllAssetsAtPath ("./Assets/_ThirdParty/Photon/PhotonUnityNetworking/Resources/PhotonServerSettings.asset");

    if (data == null || data.Length == 0) {
        Debug.Log ("cannot read content from PhotonServerSettings.asset");
        return;
    }
    Photon.Pun.ServerSettings settings = data [0] as Photon.Pun.ServerSettings;

    settings.AppSettings.Server = "10.10.10.10";
    settings.AppSettings.Port = 4530;
    settings.AppSettings.Protocol = ExitGames.Client.Photon.ConnectionProtocol.Tcp;
    AssetDatabase.SaveAssets ();
}

和.asset文件的内容:

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_PrefabParentObject: {fileID: 0}
  m_PrefabInternal: {fileID: 0}
  m_GameObject: {fileID: 0}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 9f3758f8f58fdef43803eb9be1df0608, type: 3}
  m_Name: PhotonServerSettings
  m_EditorClassIdentifier: 
  AppSettings:
    AppIdRealtime: it can be empty
    AppIdChat: 
    AppIdVoice: 
    AppVersion: 
    UseNameServer: 0
    FixedRegion: 
    Server: 20.10.10.10
    Port: 4530
    Protocol: 1
    EnableLobbyStatistics: 0
    NetworkLogging: 1
  StartInOfflineMode: 0
  PunLogging: 0
  EnableSupportLogger: 0
  RunInBackground: 1
  RpcList:
  - DestroySpaceship
  - Fire
  - ReceivePosition
  - RespawnSpaceship
  - ReceiveDressUpCommand
  - ReceiveLoadDressUpCommand
  - ReceiveSendLoginCommand
  - ReceiveSetActiveMyself
  DisableAutoOpenWizard: 1
  ShowSettings: 1

当我单击切换到开发按钮时,内容无法成功更新。我还尝试添加以下代码:

EditorUtility.SetDirty(data[0]);

也无法成功更新。如果无法使用此方法,我将使用StreamWriter和StreamReader更新txt文件。...

[编辑版本]它根本无法工作。

[MenuItem("TEST/Switch Environment/Switch to Prod")]
public static void Switch2Prod()
{
    UnityEngine.Object []data = AssetDatabase.LoadAllAssetsAtPath("./Assets/_ThirdParty/Photon/PhotonUnityNetworking/Resources/PhotonServerSettings.asset");

    if (data == null || data.Length == 0) {
        GVRChat.Log ("cannot read content from PhotonServerSettings.asset");
        return;
    }
    EditorUtility.SetDirty(data[0]);
    Photon.Pun.ServerSettings settings = data [0] as Photon.Pun.ServerSettings;
    settings.AppSettings.Server = "2.2.2.2";
    settings.AppSettings.Port = 4530;
    settings.AppSettings.Protocol = ExitGames.Client.Photon.ConnectionProtocol.Tcp;

    AssetDatabase.SaveAssets ();
    AssetDatabase.Refresh ();
}

0 个答案:

没有答案