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;

}

}

效果如:

相关推荐
星星火柴93613 分钟前
开发笔记 | 实现人物立绘的差分效果
笔记·unity·游戏程序·优香
17 小时前
3D碰撞检测系统 基于SAT算法+Burst优化(Unity)
算法·3d·unity·c#·游戏引擎·sat
dzj20211 天前
Unity是如何把3D场景显示到屏幕上的——Unity的渲染过程
3d·unity·游戏引擎·渲染·图形学
死也不注释2 天前
【鸡零狗碎记录】
unity·c#
★YUI★2 天前
学习游戏制作记录(剑投掷技能)7.26
学习·游戏·unity·c#
★YUI★3 天前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
不绝1913 天前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
玩代码3 天前
Unity里的加力
开发语言·unity
贵州晓智信息科技3 天前
Unity 性能优化全攻略
unity·性能优化·游戏引擎
UWA3 天前
UWA DAY 2025 游戏开发者大会|全议程
游戏·unity·性能优化·游戏开发·uwa·unreal engine