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;

}

}

效果如:

相关推荐
切韵10 天前
Unity编辑器扩展:UI绑定复制工具
ui·unity·编辑器
10 天前
Lua复习之何为闭包
开发语言·unity·游戏引擎·lua·交互
深空数字孪生10 天前
2025年小程序地图打车的5大技术革新:实时路况预测与智能调度升级
大数据·人工智能·unity·性能优化·小程序·游戏引擎
RPGMZ10 天前
RPGMZ 游戏引擎如何与lua进行互相调用 初探
开发语言·javascript·游戏引擎·lua·rpgmz
程序猿多布10 天前
Unity Addressable使用之检测更新流程
unity·addressable
Bunny Chen11 天前
Unity中的物理单位是真实的吗?
unity·游戏引擎
benben04411 天前
Unity3D仿星露谷物语开发69之动作声音
游戏·ui·unity·c#·游戏引擎
徐子竣11 天前
Unity Shader开发-着色器变体(1)-着色器变体概述
unity·游戏引擎·着色器
playmak3r12 天前
某手游cocos2dlua反编译
游戏引擎·lua·cocos2d
Magnum Lehar12 天前
wpf3d游戏引擎ProjectLayoutView实现
游戏引擎·wpf