422 lines
10 KiB
C#
422 lines
10 KiB
C#
//#define EDITOR_TEST
|
||
using UnityEngine;
|
||
using Game;
|
||
using FairyGUI;
|
||
using LuaInterface;
|
||
using System.Collections;
|
||
using UnityEngine.Networking;
|
||
using System.IO;
|
||
using taurus.client;
|
||
using taurus.unity;
|
||
|
||
public class GameApplication : MonoBehaviour
|
||
{
|
||
|
||
|
||
public static GameApplication Instance;
|
||
|
||
public bool buildApp;
|
||
public bool printLog=false;
|
||
public bool accountTest=false;
|
||
[HideInInspector]
|
||
public bool isAndroid64bit = false;
|
||
|
||
public AudioSource musicSource;
|
||
public AudioSource voiceSource;
|
||
|
||
DSLuaClient _luaClient;
|
||
public int StopMusic { get; set; }
|
||
public string GameInfo { get; set; }
|
||
|
||
int _musicValue, _soundValue;
|
||
private TestConsole _console=null;
|
||
ExceptionReporter _ex_reporter=null;
|
||
private LuaFunction _share_callback;
|
||
private LuaFunction _wxlogin_callback;
|
||
|
||
void Awake()
|
||
{
|
||
TaurusUnity.init();
|
||
|
||
#if Build_App
|
||
buildApp = true;
|
||
#endif
|
||
#if UNITY_STANDALONE_WIN
|
||
accountTest = true;
|
||
#endif
|
||
#if NOT_ACCOUT_TEST
|
||
accountTest = false;
|
||
#endif
|
||
|
||
Instance = this;
|
||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||
Application.targetFrameRate = 60;
|
||
Application.runInBackground = true;
|
||
Screen.fullScreen = true;
|
||
UnityEngine.Debug.developerConsoleVisible = false;
|
||
Input.multiTouchEnabled = false;
|
||
Stage.inst.soundVolume = 0;
|
||
MusicValue = PlayerPrefs.GetInt("music", 50);
|
||
SoundValue = PlayerPrefs.GetInt("sound", 50);
|
||
|
||
NetManager.TIMEOUT_TIME = 10;
|
||
NetManager.debug_print = false;
|
||
|
||
// UIConfig.depthSupportForPaintingMode = false;
|
||
// Singleton<LogManager>.GetInstance();
|
||
|
||
}
|
||
|
||
|
||
// Use this for initialization
|
||
void Start()
|
||
{
|
||
SDKCallBack.Instance.AuthCallback = __OnWXCallBack;
|
||
SDKCallBack.Instance.ShareCallback = _OnSharecallback;
|
||
|
||
}
|
||
|
||
internal void StartGame()
|
||
{
|
||
_luaClient = gameObject.AddComponent<DSLuaClient>();
|
||
// _console = new TestConsole();
|
||
// _ex_reporter = new ExceptionReporter();
|
||
}
|
||
|
||
public void RestartGame()
|
||
{
|
||
_luaClient = null;
|
||
Voice.CrealRecord();
|
||
//_console.Close();
|
||
//_console = null;
|
||
VerCheck.Instance.ResetGame();
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
TaurusUnity.update();
|
||
__PlayMusic();
|
||
//if (_console != null)
|
||
//{
|
||
// if (printLog)
|
||
// {
|
||
// _console.Open();
|
||
// }
|
||
// else
|
||
// {
|
||
// _console.Close();
|
||
// }
|
||
//}
|
||
|
||
}
|
||
|
||
#if EDITOR_TEST
|
||
//void OnApplicationFocus(bool state)
|
||
//{
|
||
// if (state)
|
||
// {
|
||
// if (_luaClient != null) _luaClient.OnApplicationActive();
|
||
// }
|
||
// else
|
||
// {
|
||
// //BestHTTP.HTTPManager.ClearRequestQueue();
|
||
// if (_luaClient != null)
|
||
// _luaClient.OnApplicationPause1();
|
||
// }
|
||
//}
|
||
#else
|
||
|
||
void OnApplicationFocus(bool state)
|
||
{
|
||
if (state)
|
||
{
|
||
Debug.Log("App resumed – clearing stale HTTP connections");
|
||
BestHTTP.HTTPManager.MaxConnectionIdleTime = System.TimeSpan.Zero;
|
||
BestHTTP.HTTPManager.ClearRequestQueue();
|
||
//if (_luaClient != null) _luaClient.OnApplicationActive();
|
||
}
|
||
else
|
||
{
|
||
//if (_luaClient != null) _luaClient.OnApplicationPause1();
|
||
}
|
||
}
|
||
|
||
void OnApplicationPause(bool state)
|
||
{
|
||
#if UNITY_IPHONE || UNITY_ANDROID || UNITY_STANDALONE
|
||
if (!state)
|
||
{
|
||
if (_luaClient != null) _luaClient.OnApplicationActive();
|
||
}
|
||
else
|
||
{
|
||
Debug.Log("App resumed – clearing stale HTTP connections");
|
||
BestHTTP.HTTPManager.MaxConnectionIdleTime = System.TimeSpan.Zero;
|
||
BestHTTP.HTTPManager.ClearRequestQueue();
|
||
//NetManager.KillAllConnection();
|
||
if (_luaClient != null)
|
||
_luaClient.OnApplicationPause1();
|
||
}
|
||
#endif
|
||
}
|
||
#endif
|
||
|
||
void OnApplicationQuit()
|
||
{
|
||
TaurusUnity.destroy();
|
||
}
|
||
|
||
public void UnExtendLua(LuaTable game_data)
|
||
{
|
||
var bundle = (string)game_data["bundle"];
|
||
var asset_path = bundle.Replace('.', '/');
|
||
|
||
#if UNITY_EDITOR && !PACK
|
||
var lua_dir = LuaConst.luaDir + "/extend_project/";
|
||
#else
|
||
var lua_dir = LuaConst.luaResDir+"/";
|
||
#endif
|
||
var luas = Directory.GetFiles(lua_dir+ asset_path, "*.lua", SearchOption.AllDirectories);
|
||
|
||
foreach(string lua in luas)
|
||
{
|
||
var moduleFileName = bundle+"." + Path.GetFileNameWithoutExtension(lua);
|
||
((DSLuaClient)LuaClient.Instance).Reload(moduleFileName);
|
||
}
|
||
}
|
||
|
||
public void ShareLink(int id,string json_data, LuaFunction callback)
|
||
{
|
||
if (!buildApp) return;
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
return;
|
||
#else
|
||
_share_callback = callback;
|
||
#if UNITY_IPHONE
|
||
PlatformIOS.ShareLink(id, json_data);
|
||
#elif UNITY_ANDROID
|
||
PlatformAndroid.Instance.ShareLink(id, json_data);
|
||
#endif
|
||
#endif
|
||
}
|
||
|
||
void _OnSharecallback()
|
||
{
|
||
if (_share_callback != null)
|
||
{
|
||
_share_callback.Call(1);
|
||
_share_callback.Dispose();
|
||
}
|
||
}
|
||
|
||
public void CopyToClipboard(string input)
|
||
{
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
TextEditor t = new TextEditor();
|
||
t.text = input;
|
||
t.OnFocus();
|
||
t.Copy();
|
||
#elif UNITY_IPHONE
|
||
PlatformIOS.CopyToClipboard(input);
|
||
#elif UNITY_ANDROID
|
||
PlatformAndroid.Instance.CopyTextToClipboard(input);
|
||
#endif
|
||
|
||
}
|
||
|
||
|
||
public int GetBatteryLevel()
|
||
{
|
||
if (!buildApp) return 100;
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
return 100;
|
||
#else
|
||
#if UNITY_IPHONE
|
||
return Mathf.FloorToInt(PlatformIOS.GetBatteryLevel() * 100);
|
||
#elif UNITY_ANDROID
|
||
var lev = Mathf.FloorToInt(PlatformAndroid.Instance.GetBatteryLevel());
|
||
return lev;
|
||
#endif
|
||
#endif
|
||
}
|
||
|
||
public void InitBugly(string androidID,string iosID){
|
||
|
||
if (_ex_reporter != null) {
|
||
_ex_reporter.init (androidID,iosID);
|
||
}
|
||
}
|
||
|
||
public void SetBuglyUserID(string userid){
|
||
|
||
if (_ex_reporter != null) {
|
||
_ex_reporter.setUserID (userid);
|
||
}
|
||
}
|
||
|
||
public void AddBuglyUserValue(string key,string value){
|
||
|
||
if (_ex_reporter != null) {
|
||
_ex_reporter.AddUserValue(key, value);
|
||
}
|
||
}
|
||
|
||
void __OnWXCallBack(int result, string json)
|
||
{
|
||
if (_wxlogin_callback != null)
|
||
{
|
||
_wxlogin_callback.Call(result, json);
|
||
if (result != 10) _wxlogin_callback.Dispose();
|
||
}
|
||
if (result != 10) _wxlogin_callback = null;
|
||
}
|
||
|
||
public void SetAppInfo(string data)
|
||
{
|
||
|
||
Hashtable map = (Hashtable)MiniJSON.Json.Deserialize(RSAHelper.Decrypt(data));
|
||
Debug.Log($"appinfo2:{map["appid"]},{map["appSecret"]}");
|
||
WXData.SetAppInfo(map["appid"] as string,map["appSecret"] as string);
|
||
|
||
#if UNITY_ANDROID
|
||
PlatformAndroid.Instance.SetAppInfo(map["appid"] as string);
|
||
#endif
|
||
}
|
||
|
||
public void WXLogin(LuaFunction callback)
|
||
{
|
||
if (!buildApp) return;
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
var json = "{\"headimgurl\":\"\",\"unionid\":\"mwxtest1000001\",\"\":1,\"nickname\":\"mwxtest1000001\",\"sex\":2}";
|
||
callback.Call(0, json);
|
||
#else
|
||
_wxlogin_callback = callback;
|
||
#if UNITY_IPHONE
|
||
PlatformIOS.WXLogin();
|
||
#elif UNITY_ANDROID
|
||
PlatformAndroid.Instance.WXLogin();
|
||
#endif
|
||
#endif
|
||
}
|
||
|
||
public string GetRoomID()
|
||
{
|
||
#if UNITY_EDITOR || UNITY_STANDALONE
|
||
return string.Empty;
|
||
#else
|
||
#if UNITY_IPHONE
|
||
return PlatformIOS.GetRoomID();
|
||
#elif UNITY_ANDROID
|
||
return PlatformAndroid.Instance.GetRoomID();
|
||
#endif
|
||
#endif
|
||
}
|
||
|
||
|
||
public void PlayMuisc(string path)
|
||
{
|
||
var clip = ResourcesManager.LoadObject<AudioClip>(path);
|
||
musicSource.clip = clip;
|
||
}
|
||
|
||
public void PlayMuisc(string groupName,string path)
|
||
{
|
||
var clip = ResourcesManager.LoadObjectByGroup<AudioClip>(path,groupName);
|
||
musicSource.clip = clip;
|
||
}
|
||
|
||
public void PlayVoice(AudioClip clip)
|
||
{
|
||
voiceSource.PlayOneShot(clip, 1f);
|
||
}
|
||
|
||
public void PlaySound(string path)
|
||
{
|
||
if (_soundValue < 5) return;
|
||
var clip = ResourcesManager.LoadObject<AudioClip>(path);
|
||
Stage.inst.PlayOneShotSound(clip);
|
||
}
|
||
|
||
public void PlaySound(string groupName, string path)
|
||
{
|
||
if (_soundValue < 5) return;
|
||
var clip = ResourcesManager.LoadObjectByGroup<AudioClip>(path, groupName);
|
||
Stage.inst.PlayOneShotSound(clip);
|
||
}
|
||
|
||
void __PlayMusic()
|
||
{
|
||
if (_musicValue >= 5)
|
||
{
|
||
if (StopMusic > 0)
|
||
{
|
||
if (musicSource.isPlaying) musicSource.Pause();
|
||
}
|
||
else
|
||
{
|
||
if (!musicSource.isPlaying) musicSource.Play();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (musicSource.isPlaying) musicSource.Stop();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public int MusicValue
|
||
{
|
||
get
|
||
{
|
||
return _musicValue;
|
||
}
|
||
set
|
||
{
|
||
if (_musicValue != value)
|
||
{
|
||
_musicValue = value;
|
||
musicSource.volume = _musicValue / 100f;
|
||
PlayerPrefs.SetInt("music", _musicValue);
|
||
PlayerPrefs.Save();
|
||
}
|
||
}
|
||
}
|
||
|
||
public int SoundValue
|
||
{
|
||
get
|
||
{
|
||
return _soundValue;
|
||
}
|
||
set
|
||
{
|
||
if (_soundValue != value)
|
||
{
|
||
_soundValue = value;
|
||
Stage.inst.soundVolume = _soundValue / 100f;
|
||
PlayerPrefs.SetInt("sound", _soundValue);
|
||
PlayerPrefs.Save();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void InitBaiduMap(string id)
|
||
{
|
||
#if UNITY_ANDROID
|
||
|
||
#elif UNITY_IPHONE
|
||
#endif
|
||
}
|
||
|
||
[LuaInterface.NoToLua]
|
||
/// <summary>
|
||
/// 版本号
|
||
/// </summary>
|
||
public static readonly Version AppVersion = new Version("1.0.0");
|
||
|
||
/// <summary>
|
||
/// 审核用于隐藏相关内容
|
||
/// </summary>
|
||
public static bool HideSdk = false;
|
||
} |