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;

}

}

效果如:

相关推荐
开维游戏引擎3 小时前
AI自动生成游戏时,deepseek和mimo对比
android·游戏·语言模型·游戏引擎·ai编程
avi91115 小时前
Unity 商业插件之(四)粒子系统,古法射击子弹轨迹 ,附加:HDRP Built-in Particle Shaders 最新的高级管线粒子Shader
unity·游戏引擎·粒子系统·particle·拖尾效果
晓131311 小时前
【Cocos Creator 3.x】篇——第四章 子系统
前端·javascript·游戏引擎
魔士于安12 小时前
Shader forge技术美术专用
游戏·unity·游戏引擎·贴图·技术美术·模型
一锅炖出任易仙14 小时前
创梦汤锅学习日记day34
学习·游戏引擎
Y学院14 小时前
C#游戏脚本开发全流程(Unity通用完整版)
游戏·unity·c#
ellis197015 小时前
Lua的module和require
unity·lua
淡海水16 小时前
38-Hybrid生态-LeanCLR总览
unity·架构·c#·热更新·clr·hybrid·leanclr
郝学胜-神的一滴17 小时前
[简化版 GAMES 101] 计算机图形学 13:从光栅化到着色——赋予三维像素光影灵魂
c++·计算机视觉·unity·godot·图形渲染·opengl·unreal
晓131317 小时前
【Cocos Creator 3.x】篇——第五章 项目实战优化技术
前端·javascript·游戏引擎