Unity InputField + ScrollRect实现微信聊天输入框功能

1、实现动态高度尺寸的的InputField

通过这两个部件就可以实现inputField的动态改变尺寸。

将inputField放入到scrollview当中作为子类

将scrollview 链接到UIChatInputField脚本中。

2、实现UIChatInputField

//聊天输入框(类似wechat)

RequireComponent(typeof(InputField))

public class UIChatInputField : MonoBehaviour,IDragHandler,IBeginDragHandler,IEndDragHandler

{

Header("尺寸设置")

public float maxHeight = 500; // 最大可见高度

public float padding = 10f; // 文本边距

public float sensitivity = 1f;// 滚动灵敏度

public float elasticStrength = 0.2f; // 边界弹性强度

public ScrollRect ScrollRect;

private RectTransform scrollRectCotent;

private RectTransform inputRect;

private ContentSizeFitter contentSizeFitter;

private InputField inputField;

private Vector2 startPos;

private bool isDragging;

public void Awake()

{

inputField = GetComponent<InputField>();

inputField.onValueChanged.AddListener(OnTextValueChanged);

inputRect = GetComponent<RectTransform>();

scrollRectCotent = ScrollRect.content;

contentSizeFitter = GetComponent<ContentSizeFitter>();

}

Vector2 normalizedPos = new Vector2(0, 0);

void OnTextValueChanged(string str)

{

contentSizeFitter.SetLayoutVertical();

Vector2 sizeDelta = inputRect.sizeDelta;

ScrollRect.content.sizeDelta = sizeDelta;

if (inputRect.sizeDelta.y >= maxHeight)

{

sizeDelta.y = maxHeight;

}

RectTransform rectsCroll = ScrollRect.GetComponent<RectTransform>();

rectsCroll.sizeDelta = sizeDelta;

ScrollRect.normalizedPosition = normalizedPos;

}

private float GetScrollRange()

{

return Mathf.Max(0, scrollRectCotent.rect.height - ScrollRect.viewport.rect.height);

}

//坐标转换

public Vector2 ConvertScreenPosition(Vector2 screenPos)

{

Vector2 localPos;

RectTransformUtility.ScreenPointToLocalPointInRectangle(ScrollRect.viewport, screenPos, null, out localPos);

return localPos;

}

public void OnBeginDrag(PointerEventData eventData)

{

isDragging = true;

startPos = ConvertScreenPosition(eventData.position);

ScrollRect.StopMovement();

}

public void OnDrag(PointerEventData eventData)

{

if (!isDragging)

return;

Vector2 currentPos = ConvertScreenPosition(eventData.position);

Vector2 delta = (currentPos - startPos)*sensitivity;

startPos = currentPos;

// 转换为标准化位置

float newNormalizedPos = ScrollRect.verticalNormalizedPosition + delta.y / GetScrollRange();

newNormalizedPos = Mathf.Clamp01(newNormalizedPos); // 基础限制

// 应用弹性边界

if (newNormalizedPos < 0 || newNormalizedPos > 1)

{

newNormalizedPos += (newNormalizedPos > 1 ? -1 : 1) * elasticStrength;

}

// 更新滚动位置

ScrollRect.verticalNormalizedPosition = newNormalizedPos;

}

public void OnEndDrag(PointerEventData eventData)

{

isDragging = false;

}

}

效果如:

相关推荐
FAREWELL0007521 分钟前
Unity基础学习(九)输入系统全解析:鼠标、键盘与轴控制
学习·unity·c#·游戏引擎
敲代码的 蜡笔小新4 小时前
【行为型之策略模式】游戏开发实战——Unity灵活算法架构的核心实现策略
unity·设计模式·c#·策略模式
Magnum Lehar1 天前
3d游戏引擎的Utilities模块实现下
c++·算法·游戏引擎
Flamesky1 天前
Unity编辑器重新编译代码
unity·重新编译
虾球xz1 天前
游戏引擎学习第277天:稀疏实体系统
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
虾球xz2 天前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
qq_5982117572 天前
Unity.UGUI DrawCall合批笔记
笔记·unity·游戏引擎
南玖yy2 天前
C/C++ 内存管理深度解析:从内存分布到实践应用(malloc和new,free和delete的对比与使用,定位 new )
c语言·开发语言·c++·笔记·后端·游戏引擎·课程设计