void OnItemCountChangeRequested(int newCount) { // Generating some random models var newModels = new ChatMessageModel[newCount]; for (int i = 0; i < newCount; ++i) { newModels[i] = CreateNewModel(i, i != 1); // the second model will always have an image, for demo purposes } _Params.Data.Clear(); _Params.Data.AddRange(newModels); ResetItems(_Params.Data.Count, true); }
/// <summary>Utility getting rid of the need of manually writing assignments</summary> public void UpdateFromModel(ChatMessageModel model, MyParams parameters) { timeText.text = model.TimestampAsDateTime.ToString("HH:mm"); string messageText = "[#" + ItemIndex + "] " + model.Text; if (text.text != messageText) { text.text = messageText; } leftIcon.gameObject.SetActive(!model.IsMine); rightIcon.gameObject.SetActive(model.IsMine); if (model.ImageIndex < 0) { image.gameObject.SetActive(false); } else { image.gameObject.SetActive(true); image.sprite = parameters.availableChatImages[model.ImageIndex]; } if (model.IsMine) { messageContentPanelImage.rectTransform.pivot = new Vector2(1.4f, .5f); messageContentPanelImage.color = new Color(.75f, 1f, 1f, colorAtInit.a); _RootLayoutGroup.childAlignment = _MessageContentLayoutGroup.childAlignment = text.alignment = TextAnchor.MiddleRight; _RootLayoutGroup.padding.right = paddingAtIconSide; _RootLayoutGroup.padding.left = paddingAtOtherSide; } else { messageContentPanelImage.rectTransform.pivot = new Vector2(-.4f, .5f); messageContentPanelImage.color = colorAtInit; _RootLayoutGroup.childAlignment = _MessageContentLayoutGroup.childAlignment = text.alignment = TextAnchor.MiddleLeft; _RootLayoutGroup.padding.right = paddingAtOtherSide; _RootLayoutGroup.padding.left = paddingAtIconSide; } }
/// <inheritdoc/> protected override void UpdateViewsHolder(ChatMessageViewsHolder newOrRecycled) { // Initialize the views from the associated model ChatMessageModel model = _Params.Data[newOrRecycled.ItemIndex]; newOrRecycled.UpdateFromModel(model, _Params); if (newOrRecycled.contentSizeFitter.enabled) { newOrRecycled.contentSizeFitter.enabled = false; } if (model.HasPendingVisualSizeChange) { // Height will be available before the next 'twin' pass, inside OnItemHeightChangedPreTwinPass() callback (see above) newOrRecycled.MarkForRebuild(); // will enable the content size fitter //newOrRecycled.contentSizeFitter.enabled = true; ScheduleComputeVisibilityTwinPass(true); } if (!newOrRecycled.IsPopupAnimationActive && newOrRecycled.itemIndexInView == GetItemsCount() - 1) // only animating the last one { newOrRecycled.IsPopupAnimationActive = true; } }