Skip to content

Commit 9fa1538

Browse files
committed
-
1 parent c5e19b0 commit 9fa1538

3 files changed

Lines changed: 65 additions & 12 deletions

File tree

Plugins/CS/NativeShare.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using AOT;
4+
using UnityEngine;
45

56
namespace iOSNativePlugin
67
{
@@ -40,9 +41,9 @@ public static void SaveImageToAlbum(string imagePath, Action<bool> callback = nu
4041
NativeShare_SaveImageToAlbum(imagePath, OnShareCloseCallback);
4142
OnSaveImageToAlbumCallback = callback;
4243
}
43-
44-
45-
44+
45+
46+
4647
/// <summary>
4748
/// 调用系统分享功能
4849
/// </summary>
@@ -52,15 +53,28 @@ public static void SaveImageToAlbum(string imagePath, Action<bool> callback = nu
5253
/// <param name="closeCallback">用户关闭分享面板的回调</param>
5354
public static void Share(string message, string url = "", string imagePath = "", Action closeCallback = null)
5455
{
55-
NativeShare_Share(message, url, imagePath, OnShareCloseCallback);
56+
var pos = NativeUI.UnityViewSize;
57+
pos.x /= 2; // 将对话框放置在屏幕底部中间位置
58+
Share(message, url, imagePath, pos, closeCallback);
59+
}
60+
public static void Share(string message, string url = "", string imagePath = "", Vector2 pos = default(Vector2), Action closeCallback = null)
61+
{
5662
OnShareClose = closeCallback;
63+
NativeShare_Share(message, url, imagePath, pos.x, pos.y, OnShareCloseCallback);
5764
}
65+
5866
/// <summary>
5967
/// 调用系统分享功能
6068
/// </summary>
6169
/// <param name="closeCallback">用户关闭分享面板的回调</param>
6270
/// <param name="shareObjects">分享内容</param>
63-
public static void ShareObjects(Action closeCallback = null, params ShareObject[] shareObjects)
71+
public static void ShareObjects(Action closeCallback = null, params ShareObject[] shareObjects)
72+
{
73+
var pos = NativeUI.UnityViewSize;
74+
pos.x /= 2; // 将对话框放置在屏幕底部中间位置
75+
ShareObjects(closeCallback, pos, shareObjects);
76+
}
77+
public static void ShareObjects(Action closeCallback = null, Vector2 pos = default(Vector2), params ShareObject[] shareObjects)
6478
{
6579
if(shareObjects == null || shareObjects.Length <= 0)
6680
return;
@@ -72,7 +86,7 @@ public static void ShareObjects(Action closeCallback = null, params ShareObject[
7286
objectsArray[i] = shareObjects[i];
7387
}
7488

75-
NativeShare_ShareObjects(objectsArray, objectsArray.Length, OnShareCloseCallback);
89+
NativeShare_ShareObjects(objectsArray, objectsArray.Length, pos.x, pos.y, OnShareCloseCallback);
7690
OnShareClose = closeCallback;
7791
}
7892
static event Action OnShareClose;

Plugins/CS/NativeUI.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public static class NativeUI
2222
[DllImport("__Internal")] static extern void NativeUI_SetStatusBarStyle(int style, bool animated);
2323
[DllImport("__Internal")] static extern void NativeUI_ShowDialog(string title, string message, string[] actions, int count, int style, double posX, double posY, DialogSelectionCallback callback);
2424

25+
26+
/// <summary>
27+
/// 获取游戏的iOS视图大小,用于与iOS原生UI进行交互、计算
28+
/// </summary>
29+
public static Vector2 UnityViewSize
30+
{
31+
get
32+
{
33+
double x = 0, y = 0;
34+
NativeUI_GetUnityViewSize(ref x, ref y);
35+
return new Vector2((float)x, (float)y);
36+
}
37+
}
38+
2539
public static bool HideHomeIndicator
2640
{
2741
get
@@ -164,7 +178,7 @@ public static void ShowTempMessage(string alertString, int duration = 5)
164178
}
165179

166180

167-
181+
168182
/// <summary>
169183
/// 显示一个对话框,允许用户进行回应
170184
/// </summary>
@@ -174,6 +188,31 @@ public static void ShowTempMessage(string alertString, int duration = 5)
174188
/// <param name="style">对话框样式(UIAlertControllerStyle)</param>
175189
/// <param name="actions">(params数组)对话框选项(action)<para><b>注 - UIAlertActionStyle会影响最终呈现在玩家屏幕上的选项顺序,但不会影响回调中的index顺序</b></para></param>
176190
public static void ShowDialog(string title, string message, Action<int> callback, UIAlertControllerStyle style, params UIAlertAction[] actions)
191+
{
192+
var pos = UnityViewSize;
193+
pos.x /= 2; // 将对话框放置在屏幕底部中间位置
194+
ShowDialog(title, message, callback, style, pos, actions);
195+
}
196+
197+
public static void ShowAlert(string title, string message, Action<int> callback, params UIAlertAction[] actions)
198+
{
199+
ShowDialog(title, message, callback, UIAlertControllerStyle.Alert, Vector2.zero, actions);
200+
}
201+
202+
public static void ShowActionSheet(string title, string message, Action<int> callback, params UIAlertAction[] actions)
203+
{
204+
var pos = UnityViewSize;
205+
pos.x /= 2; // 将对话框放置在屏幕底部中间位置
206+
ShowActionSheet(title, message, callback, pos, actions);
207+
}
208+
209+
210+
public static void ShowActionSheet(string title, string message, Action<int> callback, Vector2 pos, params UIAlertAction[] actions)
211+
{
212+
ShowDialog(title, message, callback, UIAlertControllerStyle.ActionSheet, pos, actions);
213+
}
214+
215+
static void ShowDialog(string title, string message, Action<int> callback, UIAlertControllerStyle style, Vector2 pos, params UIAlertAction[] actions)
177216
{
178217
if(actions == null || actions.Length <= 0)
179218
return;
@@ -185,7 +224,7 @@ public static void ShowDialog(string title, string message, Action<int> callback
185224
actionsArray[i] = actions[i];
186225
}
187226

188-
NativeUI_ShowDialog(title, message, actionsArray, actions.Length, (int)style, OnDialogSelectionCallback);
227+
NativeUI_ShowDialog(title, message, actionsArray, actions.Length, (int)style, pos.x, pos.y, OnDialogSelectionCallback);
189228
ShowDialogCallback = callback;
190229
}
191230

Plugins/Native/Headers/NativeShare.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ extern "C"
2525
NSMutableArray<NSString*>* array = [NSMutableArray new];
2626

2727
if(message != nil)
28-
[array addObject:[NSString stringWithFormat:@"0%@", message]];
28+
[array addObject:[NSString stringWithFormat:@"0%@", [NSString stringWithUTF8String:message]]];
2929

3030
if(url != nil)
31-
[array addObject:[NSString stringWithFormat:@"1%@", url]];
31+
[array addObject:[NSString stringWithFormat:@"1%@", [NSString stringWithUTF8String:url]]];
3232

33-
if(imgPath != nil)
34-
[array addObject:[NSString stringWithFormat:@"2%@", imgPath]];
33+
if(imagePath != nil)
34+
[array addObject:[NSString stringWithFormat:@"2%@", [NSString stringWithUTF8String:imagePath]]];
3535

3636

3737
[NativeShare ShareObject:array posX:posY posY:posY callback:callback];

0 commit comments

Comments
 (0)