获取用户信息
游戏初始化
WX.InitSDK((code) =>
{
// 打印屏幕信息
var systemInfo = WX.GetSystemInfoSync();
Debug.Log($"{systemInfo.screenWidth}:{systemInfo.screenHeight}, {systemInfo.windowWidth}:{systemInfo.windowHeight}, {systemInfo.pixelRatio}");
// 创建用户信息获取按钮,在底部区域创建一个300高度的透明区域
// 首次获取会弹出用户授权窗口, 可通过右上角-设置-权限管理用户的授权记录
var canvasWith = (int)(systemInfo.screenWidth * systemInfo.pixelRatio);
var canvasHeight = (int)(systemInfo.screenHeight * systemInfo.pixelRatio);
var buttonHeight = (int)(canvasWith / 1080f * 300f);
infoButton = WX.CreateUserInfoButton(0, canvasHeight - buttonHeight, canvasWith, buttonHeight, "zh_CN", false);
infoButton.OnTap((userInfoButonRet) =>
{
Debug.Log(JsonUtility.ToJson(userInfoButonRet.userInfo));
txtUserInfo.text = $"nickName:{userInfoButonRet.userInfo.nickName}, avartar:{userInfoButonRet.userInfo.avatarUrl}";
//userInfoButonRet.userInfo.nickName 获取用户昵称
//userInfoButonRet.userInfo.avatarUrl 获取用户图片
});
Debug.Log("infoButton Created");
});
游戏分享
public void OnShareClick()
{
WX.ShareAppMessage(new ShareAppMessageOption()
{
title = "分享标题xxx",
imageUrl = "https://inews.gtimg.com/newsapp_bt/0/12171811596_909/0",
});
}
利用PlayerPref数据持久化
和在Unity中使用方法差不多
// 注意!!! PlayerPrefs为同步接口,iOS高性能模式下为"跨进程"同步调用,会阻塞游戏线程,请避免频繁调用
PlayerPrefs.SetString("mystringkey", "myestringvalue");
PlayerPrefs.SetInt("myintkey", 123);
PlayerPrefs.SetFloat("myfloatkey", 1.23f);
Debug.Log($"PlayerPrefs mystringkey:{PlayerPrefs.GetString("mystringkey")}");
Debug.Log($"PlayerPrefs myintkey:{PlayerPrefs.GetInt("myintkey")}");
Debug.Log($"PlayerPrefs myfloatkey:{PlayerPrefs.GetFloat("myfloatkey")}");
文本输入框调用手机键盘输入
将脚本挂载在InputField上即可。
public class Inputs : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
{
public InputField input;
public Text text;
private bool isShowKeyboad = false;
public void OnPointerClick(PointerEventData eventData)
{
ShowKeyboad();
}
public void OnPointerExit(PointerEventData eventData)
{
// 隐藏输入法
if (!input.isFocused)
{
HideKeyboad();
}
}
public void OnInput(OnKeyboardInputListenerResult v)
{
Debug.Log("onInput");
Debug.Log(v.value);
text.text = v.value;
if (input.isFocused)
{
input.text = v.value;
}
}
public void OnConfirm(OnKeyboardInputListenerResult v)
{
// 输入法confirm回调
Debug.Log("onConfirm");
Debug.Log(v.value);
HideKeyboad();
}
public void OnComplete(OnKeyboardInputListenerResult v)
{
// 输入法complete回调
Debug.Log("OnComplete");
Debug.Log(v.value);
HideKeyboad();
}
private void ShowKeyboad()
{
if (!isShowKeyboad)
{
WX.ShowKeyboard(new ShowKeyboardOption()
{
defaultValue = "",
maxLength = 20,
confirmType = "go"
});
//绑定回调
WX.OnKeyboardConfirm(OnConfirm);
WX.OnKeyboardComplete(OnComplete);
WX.OnKeyboardInput(OnInput);
isShowKeyboad = true;
}
}
private void HideKeyboad()
{
if (isShowKeyboad)
{
WX.HideKeyboard(new HideKeyboardOption());
//删除掉相关事件监听
WX.OffKeyboardInput(OnInput);
WX.OffKeyboardConfirm(OnConfirm);
WX.OffKeyboardComplete(OnComplete);
isShowKeyboad = false;
}
}
}
音效控制
和在Unity中使用方法差不多
public class AudioManager : MonoBehaviour
{
public AudioSource AudioSource;
public AudioClip AudioClipLong;
public AudioClip AudioClipShort;
public Slider VolumeSlider;
private void Start()
{
VolumeSlider.value = AudioSource.volume;
VolumeSlider.onValueChanged.AddListener((_) =>
{
OnVolumeChanged(_);
});
}
void OnVolumeChanged(float volume)
{
AudioSource.volume = volume;
}
/// <summary>
/// 循环播放
/// </summary>
public void PlayLong()
{
AudioSource.clip = AudioClipLong;
AudioSource.loop = true;
AudioSource.Play();
}
/// <summary>
/// 播放一次
/// </summary>
public void PlayShort()
{
AudioSource.clip = AudioClipShort;
AudioSource.loop = false;
AudioSource.Play();
}
/// <summary>
/// 暂停播放
/// </summary>
public void Pause()
{
AudioSource.Pause();
}
/// <summary>
/// 恢复播放
/// </summary>
public void Resume()
{
AudioSource.UnPause();
}
/// <summary>
/// 关闭声音
/// </summary>
public void Stop()
{
AudioSource.Stop();
}
}
获取微信字体
// fallbackFont作为旧版本微信或者无法获得系统字体文件时的备选CDN URL
var fallbackFont = Application.streamingAssetsPath + "/Fz.ttf";
WX.GetWXFont(fallbackFont, (font) =>
{
if (font != null)
{
txtTestWXFont.font = font;
}
});
微信文件系统存储
public void OnFileSystemManagerClick()
{
// 扫描文件系统目录
fs.Stat(new WXStatOption
{
path = env.USER_DATA_PATH + "/__GAME_FILE_CACHE",
recursive = true,
success = (succ) =>
{
Debug.Log($"stat success");
foreach (var file in succ.stats)
{
Debug.Log($"stat info. {file.path}, " +
$"{file.stats.size}," +
$"{file.stats.mode}," +
$"{file.stats.lastAccessedTime}," +
$"{file.stats.lastModifiedTime}");
}
},
fail = (fail) =>
{
Debug.Log($"stat fail {fail.errMsg}");
}
});
// 同步接口创建目录(请勿在游戏过程中频繁调用同步接口)
var errMsg = fs.MkdirSync(env.USER_DATA_PATH + "/mydir", true);
// 异步写入文件
fs.WriteFile(new WriteFileParam
{
filePath = env.USER_DATA_PATH + "/mydir/myfile.txt",
encoding = "utf8",
data = System.Text.Encoding.UTF8.GetBytes("Test FileSystemManager"),
success = (succ) =>
{
Debug.Log($"WriteFile succ {succ.errMsg}");
// 异步读取文件
fs.ReadFile(new ReadFileParam
{
filePath = env.USER_DATA_PATH + "/mydir/myfile.txt",
encoding = "utf8",
success = (succ) =>
{
Debug.Log($"ReadFile succ. stringData(utf8):{succ.stringData}");
},
fail = (fail) =>
{
Debug.Log($"ReadFile fail {fail.errMsg}");
}
});
// 异步以无编码(二进制)方式读取
fs.ReadFile(new ReadFileParam
{
filePath = env.USER_DATA_PATH + "/mydir/myfile.txt",
encoding = "",
success = (succ) =>
{
Debug.Log($"ReadFile succ. data(binary):{succ.binData.Length}, stringData(utf8):{System.Text.Encoding.UTF8.GetString(succ.binData)}");
},
fail = (fail) =>
{
Debug.Log($"ReadFile fail {fail.errMsg}");
}
});
},
fail = (fail) =>
{
Debug.Log($"WriteFile fail {fail.errMsg}");
},
complete = null
});
}
标签:Unity3D,Log,微信,void,fail,API,Debug,public,WX
From: https://www.cnblogs.com/lxp-blog/p/17588519.html