public static void ConvertRectTransformsRecursive(UnityEngine.RectTransform parent, Entity parentEntity, RectTransformToEntity rectTransformToEntity, EntityManager commandBuffer, ref DotsUIArchetypes archetypes, ref NativeList <LinkedEntityGroup> linkedGroup, bool isPrefab) { rectTransformToEntity.Add(parent, parentEntity); linkedGroup.Add(parentEntity); #if UNITY_EDITOR commandBuffer.SetName(parentEntity, parent.name); #endif m_PooledRectTransformList.Add(parent); int childCount = parent.childCount; for (int i = 0; i < childCount; i++) { if (parent.GetChild(i) is UnityEngine.RectTransform childTransform) { //if(!childTransform.gameObject.activeInHierarchy) // continue; Entity child = commandBuffer.CreateEntity(archetypes.GenericChildArchetype); SetRectTransform(commandBuffer, childTransform, child); commandBuffer.SetComponentData(child, new Parent() { Value = parentEntity }); if (isPrefab) { commandBuffer.AddComponent(child, typeof(Prefab)); } ConvertRectTransformsRecursive(childTransform, child, rectTransformToEntity, commandBuffer, ref archetypes, ref linkedGroup, isPrefab); } } }
/// <summary> /// /// </summary> /// <param name="unityPrefab"></param> /// <param name="prefabWorld">This world will keep your converter prefab for further instantiation public DotsUIPrefab(UnityEngine.RectTransform unityPrefab, EntityManager manager, Allocator allocator) { m_RectTransformToPrefabEntity = new RectTransformToEntity(16, allocator); m_Manager = manager; m_Root = RectTransformConversionUtils.ConvertPrefabHierarchy(unityPrefab, manager, m_RectTransformToPrefabEntity); m_Manager.SetEnabled(m_Root, false); }
private static void UpdateComponents(RectTransformToEntity hierarchy, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { //foreach (var (rectTransform, entity) in hierarchy) foreach (var rectTransform in m_PooledRectTransformList) { m_Converter.ConvertComponents(rectTransform, hierarchy[rectTransform], hierarchy, assetToEntity, commandBuffer); } }
/// <summary> /// Instantiate prefab and fill RectTransform to Entity map /// </summary> /// <param name="rectTransformToEntity"></param> /// <returns></returns> public Entity Instantiate(RectTransformToEntity rectTransformToEntity) { Entity instance = Instantiate(); var prefabGroup = m_Manager.GetBuffer <LinkedEntityGroup>(m_Root); var instanceGroup = m_Manager.GetBuffer <LinkedEntityGroup>(instance); for (int i = 0; i < prefabGroup.Length; i++) { rectTransformToEntity.Add(m_RectTransformToPrefabEntity[prefabGroup[i].Value], instanceGroup[i].Value); } return(instance); }
protected override void ConvertComponent(RectMask2D unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { commandBuffer.AddComponent(entity, typeof(RectMask)); }
protected override void ConvertComponent(UnityEngine.UI.CanvasScaler unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { if (unityComponent.uiScaleMode == UnityEngine.UI.CanvasScaler.ScaleMode.ConstantPhysicalSize) { var scaleFactor = 1.0f; switch (unityComponent.physicalUnit) { case CanvasScaler.Unit.Centimeters: scaleFactor = 1.0f / 2.54f; break; case CanvasScaler.Unit.Millimeters: scaleFactor = 1.0f / 25.4f; break; case CanvasScaler.Unit.Inches: scaleFactor = 1.0f / 1; break; case CanvasScaler.Unit.Points: scaleFactor = 1.0f / 72; break; case CanvasScaler.Unit.Picas: scaleFactor = 1.0f / 6; break; } commandBuffer.AddComponentData(entity, new CanvasConstantPhysicalSizeScaler() { Factor = scaleFactor }); } else if (unityComponent.uiScaleMode == UnityEngine.UI.CanvasScaler.ScaleMode.ConstantPixelSize) { commandBuffer.AddComponentData(entity, new CanvasConstantPixelSizeScaler()); } }
protected override void ConvertComponent(Canvas unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { CanvasSortLayer image = new CanvasSortLayer { Value = unityComponent.sortingOrder }; commandBuffer.AddComponentData(entity, image); commandBuffer.AddComponent(entity, typeof(RebuildCanvasHierarchyFlag)); commandBuffer.AddBuffer <MeshVertex>(entity); commandBuffer.AddBuffer <MeshVertexIndex>(entity); commandBuffer.AddBuffer <SubMeshInfo>(entity); if (unityComponent.renderMode != RenderMode.ScreenSpaceCamera) { throw new InvalidOperationException($"Canvas ({unityComponent}) render mode ({unityComponent.renderMode}) is not supported yet"); } if (unityComponent.worldCamera == null) { throw new InvalidOperationException($"Target camera is null or destroyed. Canvas {unityComponent}"); } var proxy = unityComponent.worldCamera.GetComponent <CameraImageRenderProxy>(); if (proxy == null) { proxy = unityComponent.worldCamera.gameObject.AddComponent <CameraImageRenderProxy>(); } commandBuffer.AddSharedComponentData(entity, new CanvasTargetCamera() { Target = proxy }); }
protected override void ConvertComponent(UnityEngine.UI.InputField unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { commandBuffer.AddComponentData(entity, new DotsUI.Input.KeyboardInputReceiver()); commandBuffer.AddBuffer <DotsUI.Input.KeyboardInputBuffer>(entity); Entity target = default; if (!rectTransformToEntity.TryGetValue(unityComponent.targetGraphic?.rectTransform, out target)) { target = entity; } commandBuffer.AddComponentData(entity, new DotsUI.Controls.InputField() { Target = target }); commandBuffer.AddComponentData(entity, new DotsUI.Controls.InputFieldCaretState() { CaretPosition = 0 }); }
protected override void ConvertComponent(TextMeshProUGUI unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { if (unityComponent.font == null) { Debug.LogError($"TextMeshProConverter - font asset cannot be null reference. Object: {unityComponent}", unityComponent); return; } if (!assetToEntity.TryGetValue(unityComponent.font, out var fontAsset)) { fontAsset = TextUtils.CreateFontAssetFromTmp(commandBuffer, unityComponent.font); assetToEntity.Add(unityComponent.font, fontAsset); } commandBuffer.AddComponentData(entity, new TextRenderer() { Font = fontAsset, Size = unityComponent.fontSize, Alignment = unityComponent.alignment, Bold = (unityComponent.fontStyle & FontStyles.Bold) == FontStyles.Bold, Italic = (unityComponent.fontStyle & FontStyles.Italic) == FontStyles.Italic }); var textBuffer = commandBuffer.AddBuffer <TextData>(entity); var content = unityComponent.text; textBuffer.ResizeUninitialized(content.Length); unsafe { fixed(char *textPtr = content) UnsafeUtility.MemCpy(textBuffer.GetUnsafePtr(), textPtr, content.Length * sizeof(char)); } commandBuffer.AddBuffer <ControlVertexData>(entity); commandBuffer.AddBuffer <ControlVertexIndex>(entity); commandBuffer.AddComponent(entity, typeof(RebuildElementMeshFlag)); }
public void ConvertComponents(UnityEngine.RectTransform rectTransform, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr) { foreach (var(type, converter) in m_Converters) { var component = rectTransform.GetComponent(type); if (component != null) { try { converter.Convert(component, entity, rectTransformToEntity, assetToEntity, mgr); } catch (Exception e) { UnityEngine.Debug.LogException(e); } } } var components = rectTransform.GetComponents <IRectTransformToEntity>(); foreach (var component in components) { component.ConvertToEntity(entity, rectTransformToEntity, assetToEntity, mgr); } }
internal static Entity ConvertPrefabHierarchy(UnityEngine.RectTransform root, EntityManager manager, RectTransformToEntity rectTransformToEntity) { Initialize(); var archetypes = new DotsUIArchetypes(manager); var assetToEntity = new Dictionary <UnityEngine.Object, Entity>(); var entity = manager.CreateEntity(archetypes.GenericChildArchetype); SetRectTransform(manager, root, entity); var linkedGroup = new NativeList <LinkedEntityGroup>(Allocator.Temp); ConvertRectTransformsRecursive(root.GetComponent <UnityEngine.RectTransform>(), entity, rectTransformToEntity, manager, ref archetypes, ref linkedGroup, true); manager.AddBuffer <LinkedEntityGroup>(entity).AddRange(linkedGroup.AsArray()); UpdateComponents(rectTransformToEntity, assetToEntity, manager); return(entity); }
public static void ConvertCanvasHierarchy(Canvas root, EntityManager manager) { RectTransformToEntity rectTransformToEntity = new RectTransformToEntity(10, Allocator.Temp); ConvertCanvasHierarchy(root, manager, rectTransformToEntity); }
public static void ConvertCanvasHierarchy(Canvas root, EntityManager manager, RectTransformToEntity rectTransformToEntity) { Initialize(); var archetypes = new DotsUIArchetypes(manager); var assetToEntity = new Dictionary <UnityEngine.Object, Entity>(); var entity = manager.CreateEntity(archetypes.CanvasArchetype); var linkedGroup = new NativeList <LinkedEntityGroup>(Allocator.Temp);//manager.AddBuffer<LinkedEntityGroup>(entity); linkedGroup.Add(entity); ConvertRectTransformsRecursive(root.GetComponent <UnityEngine.RectTransform>(), entity, rectTransformToEntity, manager, ref archetypes, ref linkedGroup, false); manager.AddBuffer <LinkedEntityGroup>(entity).AddRange(linkedGroup.AsArray()); UpdateComponents(rectTransformToEntity, assetToEntity, manager); }
protected override void ConvertComponent(UnityEngine.UI.Button unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { commandBuffer.AddComponentData(entity, new DotsUI.Controls.Button()); }
protected override void ConvertComponent(UnityEngine.UI.ScrollRect unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr) { mgr.AddComponentData(entity, new DotsUI.Controls.ScrollRect() { Content = rectTransformToEntity[unityComponent.content], Viewport = rectTransformToEntity[unityComponent.viewport], HorizontalBar = rectTransformToEntity[unityComponent.horizontalScrollbar.transform as UnityEngine.RectTransform], VerticalBar = rectTransformToEntity[unityComponent.verticalScrollbar.transform as UnityEngine.RectTransform], HorizontalBarSpacing = unityComponent.horizontalScrollbarSpacing, VerticalBarSpacing = unityComponent.verticalScrollbarSpacing }); var pointerInputReceiver = GetOrAddComponent <Input.PointerInputReceiver>(mgr, entity); pointerInputReceiver.ListenerTypes |= Input.PointerEventType.BeginDrag | Input.PointerEventType.Drag | Input.PointerEventType.EndDrag; mgr.SetComponentData(entity, pointerInputReceiver); }
protected override void ConvertComponent(UnityEngine.UI.Selectable unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr) { mgr.AddComponent(entity, typeof(DotsUI.Input.Selectable)); var colors = unityComponent.colors; Entity target; if (!rectTransformToEntity.TryGetValue(unityComponent.targetGraphic?.rectTransform, out target)) { target = entity; } mgr.AddComponentData(entity, new DotsUI.Input.SelectableColor() { Normal = colors.normalColor.ToFloat4(), Hover = colors.highlightedColor.ToFloat4(), Pressed = colors.pressedColor.ToFloat4(), Selected = colors.selectedColor.ToFloat4(), Disabled = colors.disabledColor.ToFloat4(), TransitionTime = colors.fadeDuration, Target = target }); var pointerInputReceiver = GetOrAddComponent <Input.PointerInputReceiver>(mgr, entity); pointerInputReceiver.ListenerTypes |= Input.PointerEventType.SelectableGroup; mgr.SetComponentData(entity, pointerInputReceiver); }
public override void Convert(Component unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr) { ConvertComponent((T)unityComponent, entity, rectTransformToEntity, assetToEntity, mgr); }
abstract protected void ConvertComponent(T unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr);
protected override void ConvertComponent(UnityEngine.UI.Graphic unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager commandBuffer) { commandBuffer.AddComponentData(entity, new VertexColorValue() { Value = unityComponent.color.ToFloat4() }); commandBuffer.AddComponentData(entity, new VertexColorMultiplier() { Value = new float4(1.0f, 1.0f, 1.0f, 1.0f) }); commandBuffer.AddComponent(entity, typeof(ElementVertexPointerInMesh)); }
public abstract void Convert(Component unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr);
protected override void ConvertComponent(UnityEngine.UI.Scrollbar unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr) { var scrollHandle = rectTransformToEntity[unityComponent.handleRect]; mgr.AddComponentData(entity, new DotsUI.Controls.ScrollBar() { ScrollHandle = scrollHandle, Value = unityComponent.value, ParentScrollRect = rectTransformToEntity[unityComponent.GetComponentsInParent <ScrollRect>(true)[0].transform as UnityEngine.RectTransform] // temporary workaround for inactive transforms }); mgr.AddComponentData(entity, new Controls.ScrollBarHandle() { }); var pointerInputReceiver = GetOrAddComponent <Input.PointerInputReceiver>(mgr, entity); pointerInputReceiver.ListenerTypes |= Input.PointerEventType.BeginDrag | Input.PointerEventType.Drag | Input.PointerEventType.EndDrag; mgr.SetComponentData(entity, pointerInputReceiver); }
protected override void ConvertComponent(TMP_InputField unityComponent, Entity entity, RectTransformToEntity rectTransformToEntity, Dictionary <UnityEngine.Object, Entity> assetToEntity, EntityManager mgr) { mgr.AddComponentData(entity, new DotsUI.Input.KeyboardInputReceiver()); mgr.AddBuffer <DotsUI.Input.KeyboardInputBuffer>(entity); Entity target = default; if (!rectTransformToEntity.TryGetValue(unityComponent.textComponent?.rectTransform, out target)) { target = entity; } Entity placeholder = default; rectTransformToEntity.TryGetValue(unityComponent.placeholder.rectTransform, out placeholder); mgr.AddComponentData(entity, new DotsUI.Controls.InputField() { Target = target, Placeholder = placeholder }); mgr.AddComponentData(entity, new DotsUI.Controls.InputFieldCaretState() { CaretPosition = 0, }); }