同步进度
This commit is contained in:
@@ -18,19 +18,21 @@ public class MonsterMoveLine
|
||||
public Vector2 end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static async void LoadBezier()
|
||||
{
|
||||
await LoadBezierTxtAsync("MoveLine");
|
||||
}
|
||||
/// <summary>
|
||||
/// 读取数据 游戏开始时在awake调用aa包
|
||||
/// 读取曲线数据 读取单个
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task LoadBezierTxtAsync(string path)
|
||||
public static async Task LoadSingleBezierTxtAsync(string path)
|
||||
{
|
||||
AsyncOperationHandle<TextAsset> handle = Addressables.LoadAssetAsync<TextAsset>(path);
|
||||
await handle.Task;
|
||||
var lines = handle.Result.text.Split('\n');
|
||||
|
||||
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
@@ -51,9 +53,12 @@ public class MonsterMoveLine
|
||||
|
||||
// 第一个点是 start
|
||||
curve.start = points[0];
|
||||
if (points.Count > 1)
|
||||
{
|
||||
// 最后一个点是 end
|
||||
curve.end = points[points.Count - 1];
|
||||
}
|
||||
|
||||
// 最后一个点是 end
|
||||
curve.end = points[points.Count - 1];
|
||||
|
||||
// 中间的都是 controlPoints
|
||||
for (int i = 1; i < points.Count - 1; i++)
|
||||
@@ -62,10 +67,64 @@ public class MonsterMoveLine
|
||||
}
|
||||
|
||||
curves.Add(curve);
|
||||
dic_BezierCurveDatas.Add(curve.name,curve);
|
||||
}
|
||||
dic_BezierCurveDatas.Add(curve.name, curve);
|
||||
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 读取曲线数据 游戏开始时在awake调用aa包
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task LoadBezierTxtAsync(string path)
|
||||
{
|
||||
AsyncOperationHandle<IList<TextAsset>> handles = Addressables.LoadAssetsAsync<TextAsset>(path,null);
|
||||
await handles.Task;
|
||||
foreach (var text in handles.Result)
|
||||
{
|
||||
var lines = text.text.Split('\n');
|
||||
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
|
||||
var parts = line.Split(';');
|
||||
BezierCurveData curve = new BezierCurveData();
|
||||
|
||||
// 第一个是曲线名
|
||||
curve.name = parts[0];
|
||||
|
||||
// 后面所有都是点
|
||||
List<Vector2> points = new List<Vector2>();
|
||||
for (int i = 1; i < parts.Length; i++)
|
||||
{
|
||||
points.Add(ParseVector(parts[i]));
|
||||
}
|
||||
|
||||
// 第一个点是 start
|
||||
curve.start = points[0];
|
||||
if (points.Count > 1)
|
||||
{
|
||||
// 最后一个点是 end
|
||||
curve.end = points[points.Count - 1];
|
||||
}
|
||||
|
||||
|
||||
// 中间的都是 controlPoints
|
||||
for (int i = 1; i < points.Count - 1; i++)
|
||||
{
|
||||
curve.controlPoints.Add(points[i]);
|
||||
}
|
||||
|
||||
curves.Add(curve);
|
||||
dic_BezierCurveDatas.Add(curve.name, curve);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log("曲线读取完成共" + dic_BezierCurveDatas.Count + "条");
|
||||
|
||||
}
|
||||
|
||||
private static Vector2 ParseVector(string s)
|
||||
@@ -83,7 +142,13 @@ public class MonsterMoveLine
|
||||
{
|
||||
this.bezierCurveData = bezierCurveData;
|
||||
this.moveTime = Time;
|
||||
Debug.Log($"初始化曲线 {this.bezierCurveData.name} ,移动时间 {moveTime}");
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取曲线上的点
|
||||
/// </summary>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public Vector2 GetBezierPoint(float t)
|
||||
{
|
||||
t = t / moveTime;
|
||||
@@ -95,6 +160,7 @@ public class MonsterMoveLine
|
||||
|
||||
return DeCasteljau(points, t%1);
|
||||
}
|
||||
|
||||
|
||||
private Vector2 DeCasteljau(List<Vector2> points, float t)
|
||||
{
|
||||
@@ -107,50 +173,9 @@ public class MonsterMoveLine
|
||||
}
|
||||
points = next;
|
||||
}
|
||||
return CanvasToWorld( points[0],UIManager.Instance.uiRoot.GetComponent<RectTransform>(),Camera.main);
|
||||
}
|
||||
public static Vector3 CanvasToWorld(Vector2 canvasPos, RectTransform canvas, Camera cam)
|
||||
{
|
||||
// 1. 获取 Canvas 实际像素尺寸(自动适配所有设备)
|
||||
float canvasWidth = canvas.rect.width;
|
||||
float canvasHeight = canvas.rect.height;
|
||||
|
||||
// 2. 正交相机的世界高度
|
||||
float worldHeight = cam.orthographicSize * 2f;
|
||||
|
||||
// 3. 世界宽度 = 世界高度 * 屏幕宽高比
|
||||
float worldWidth = worldHeight * cam.aspect;
|
||||
|
||||
// 4. 画布单位 → 世界单位比例(横竖一致)
|
||||
float scaleX = worldWidth / canvasWidth;
|
||||
float scaleY = worldHeight / canvasHeight;
|
||||
|
||||
// 5. 你的 canvasPos 是以中心为原点的坐标
|
||||
float worldX = canvasPos.x * scaleX;
|
||||
float worldY = canvasPos.y * scaleY;
|
||||
|
||||
// 6. 映射到世界 z = 0 平面
|
||||
return new Vector3(worldX, worldY, 0f);
|
||||
return CoordinateTransformation.CanvasToWorld( points[0],UIManager.Instance.uiRoot.GetComponent<RectTransform>(),Camera.main);
|
||||
}
|
||||
|
||||
public static Vector2 AnchoredPos_ToCanvas(RectTransform target, Canvas canvas)
|
||||
{
|
||||
// 1. 先把 anchoredPosition(父坐标)转换成世界坐标
|
||||
Vector3 worldPos = target.TransformPoint(target.anchoredPosition);
|
||||
|
||||
// 2. 再把世界坐标转换成 Canvas 的局部坐标(等价于 Canvas 下的 anchoredPosition)
|
||||
RectTransform canvasRT = canvas.GetComponent<RectTransform>();
|
||||
|
||||
Vector2 canvasLocalPos;
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
||||
canvasRT,
|
||||
RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, worldPos),
|
||||
canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera,
|
||||
out canvasLocalPos
|
||||
);
|
||||
|
||||
return canvasLocalPos;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user