Merge branch 'ScrollToNode功能' into 'develop'

提交scrollToNode

See merge request aibis-dream/aibis-dream!98
This commit is contained in:
2024-11-17 18:41:37 +00:00
2 changed files with 62 additions and 3 deletions
+3 -3
View File
@@ -2098,7 +2098,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 3728315363019128986, guid: c998a63bd211990449fe9bb1f2832e3d, type: 3}
propertyPath: localNodes.Array.size
value: 1
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3728315363019128986, guid: c998a63bd211990449fe9bb1f2832e3d, type: 3}
propertyPath: localNodes.Array.data[0]
@@ -2486,11 +2486,11 @@ PrefabInstance:
objectReference: {fileID: 579117716}
- target: {fileID: 6374982573157599259, guid: c998a63bd211990449fe9bb1f2832e3d, type: 3}
propertyPath: scaleDuration
value: 1
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 6374982573157599259, guid: c998a63bd211990449fe9bb1f2832e3d, type: 3}
propertyPath: scrollDuration
value: 1
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 6374982573157599259, guid: c998a63bd211990449fe9bb1f2832e3d, type: 3}
propertyPath: contentRectTransform
+59
View File
@@ -6,6 +6,8 @@ using MeadowGames.UINodeConnect4;
using UnityEngine;
using Yarn.Unity;
using RainbowArt.CleanFlatUI;
using UnityEngine.UI;
using DG.Tweening;
public class ClueBaordNew : MonoBehaviour
{
@@ -27,6 +29,12 @@ public class ClueBaordNew : MonoBehaviour
private Connection currentConnection;
private bool isTriggerDialogue=true;
public ScrollRect scrollRect; // ScrollView的ScrollRect组件
public RectTransform contentRectTransform; // Content的RectTransform
public float padding = 10f; // 节点的额外间隔
public float scrollDuration = 1f; // 滚动动画持续时间
public float scaleDuration = 1f; // 缩放动画持续时间
void Start()
{
gm=GetComponent<GraphManager>();
@@ -38,6 +46,57 @@ public class ClueBaordNew : MonoBehaviour
{
}
[YarnCommand("ScrollToNode")]
public IEnumerator ScrollToNode(string nodeName)
{
// 获取目标节点
Transform targetNode = GetNode(nodeName)?.transform;
if (targetNode == null)
{
Debug.LogWarning($"Node {nodeName} not found!");
yield break;
}
RectTransform targetRectTransform = targetNode.GetComponent<RectTransform>();
if (targetRectTransform == null)
{
Debug.LogWarning($"Node {nodeName} does not have a RectTransform!");
yield break;
}
// **核心逻辑:计算目标 Content 位置**
// 获取目标节点相对于 Content 的本地位置
Vector2 nodeLocalPos = targetRectTransform.localPosition;
// Content 的目标位置 = 目标节点相对于 Content 的位置 - Viewport 的中心偏移
Vector2 viewportCenterOffset = new Vector2(
scrollRect.viewport.rect.width / 2,
-scrollRect.viewport.rect.height / 2
);
Vector2 targetContentPos = -nodeLocalPos + viewportCenterOffset;
// 限制 Content 的移动范围,防止超出边界
// float minX = 0; // Content 左侧对齐
// float minY = -contentRectTransform.rect.height + scrollRect.viewport.rect.height; // Content 底部对齐
// float maxX = 0; // Content 不水平滚动
// float maxY = 0; // Content 顶部对齐
// targetContentPos = new Vector2(
// Mathf.Clamp(targetContentPos.x, minX, maxX),
// Mathf.Clamp(targetContentPos.y, minY, maxY)
// );
// 平滑移动 Content 到目标位置
contentRectTransform.DOAnchorPos(targetContentPos, scrollDuration).SetEase(Ease.InOutQuad);
// 缩放目标节点
targetRectTransform.DOScale(Vector3.one * 1.5f, scaleDuration).SetEase(Ease.OutBack);
// 等待动画完成
yield return new WaitForSeconds(scrollDuration);
// 恢复目标节点缩放
targetRectTransform.DOScale(Vector3.one, scaleDuration).SetEase(Ease.OutBack);
}
[YarnCommand("SetNodeState")]
public void ActiveNodeToBoard(string nodeID,bool isactive)
{