//------------------------------------------------------------------------------------------------------------------------------------------ public Vector3[] getLocalMinMaxPixelPos() { // 获得第一个带widget的父节点的rect UIRect rect = WidgetUtility.findParentRect(mObject); Vector2 parentWidgetSize = WidgetUtility.getRectSize(rect); // 计算父节点的世界缩放 Vector3 worldScale = getMatrixScale(mTransform.parent.localToWorldMatrix); txUIObject root = mLayout.isNGUI() ? mLayoutManager.getNGUIRoot() : mLayoutManager.getUGUIRoot(); Vector3 uiRootScale = root.getTransform().localScale; Vector2 parentScale = new Vector2(worldScale.x / uiRootScale.x, worldScale.y / uiRootScale.y); // 计算移动的位置范围 Vector2 minPos = new Vector2(parentWidgetSize.x / 2.0f * mMinRelativePos.x / parentScale.x, parentWidgetSize.y / 2.0f * mMinRelativePos.y / parentScale.y); Vector2 maxPos = new Vector2(parentWidgetSize.x / 2.0f * mMaxRelativePos.x / parentScale.x, parentWidgetSize.y / 2.0f * mMaxRelativePos.y / parentScale.y); if (mClampType == CLAMP_TYPE.CT_EDGE) { Vector2 thisSize = getWindowSize(true); minPos += thisSize / 2.0f; maxPos -= thisSize / 2.0f; if (!mClampInner) { swap(ref minPos, ref maxPos); } } else if (mClampType == CLAMP_TYPE.CT_CENTER) { } return(new Vector3[2] { minPos, maxPos }); }
public void updateRect(bool force = false) { // 是否为编辑器手动预览操作,手动预览不需要启动游戏 #if UNITY_EDITOR bool preview = !EditorApplication.isPlaying; #else bool preview = false; #endif // 如果是第一次更新,则需要获取原始属性 if (mFirstUpdate || preview) { mScreenScale = UnityUtility.getScreenScale(UnityUtility.getRootSize(preview)); mOriginSize = WidgetUtility.getRectSize(GetComponent <RectTransform>()); mOriginPos = transform.localPosition; mFirstUpdate = false; } if (!preview && !force && !mDirty) { return; } mDirty = false; Vector3 realScale = UnityUtility.adjustScreenScale(mScreenScale, mKeepAspect ? mAspectBase : ASPECT_BASE.NONE); float thisWidth = mOriginSize.x * realScale.x; float thisHeight = mOriginSize.y * realScale.y; MathUtility.checkInt(ref thisWidth, 0.001f); MathUtility.checkInt(ref thisHeight, 0.001f); Vector2 newSize = new Vector2(thisWidth, thisHeight); // 只有在刷新时才能确定父节点,所以父节点需要实时获取 WidgetUtility.setRectSize(GetComponent <RectTransform>(), newSize, mAdjustFont); transform.localPosition = MathUtility.round(MathUtility.multiVector3(mOriginPos, realScale)); }
public static Vector2 screenPosToWindowPos(Vector2 screenPos, txUIObject parent, bool screenCenterAsZero = false, bool isNGUI = true) { Camera camera = mCameraManager.getUICamera().getCamera(); Vector2 rootSize = WidgetUtility.getRootSize(); screenPos.x = screenPos.x / camera.pixelWidth * rootSize.x; screenPos.y = screenPos.y / camera.pixelHeight * rootSize.y; Vector3 parentWorldPosition = parent != null?parent.getWorldPosition() : Vector3.zero; txUIObject root = isNGUI ? mLayoutManager.getNGUIRoot() : mLayoutManager.getUGUIRoot(); Vector3 scale = root.getScale(); parentWorldPosition.x = parentWorldPosition.x / scale.x; parentWorldPosition.y = parentWorldPosition.y / scale.y; Vector2 parentWorldScale = parent != null?parent.getWorldScale() : Vector2.one; Vector2 pos = new Vector2(screenPos.x - parentWorldPosition.x, screenPos.y - parentWorldPosition.y); Vector2 windowPos = new Vector2(pos.x / parentWorldScale.x, pos.y / parentWorldScale.y); if (screenCenterAsZero) { windowPos -= rootSize / 2; } return(windowPos); }
protected override void applyScreenPosition(Vector3 screenPos) { txUIObject window = mComponentOwner as txUIObject; Vector2 rootSize = WidgetUtility.getRootSize(); window.setLocalPosition(UnityUtility.screenPosToWindowPos(screenPos, window.getParent()) - rootSize / 2); }
protected override void Start() { base.Start(); this.m_ResultStorageContainer = WidgetUtility.Find <ItemContainer>(this.m_ResultStorageWindow); this.m_RequiredIngredientsContainer = WidgetUtility.Find <ItemContainer>(this.m_RequiredIngredientsWindow); this.m_Progressbar = WidgetUtility.Find <Progressbar>(this.m_CraftingProgressbar); }
private void Start() { if (transform.root != transform) { EquipmentHandler handler = transform.root.gameObject.GetComponent <EquipmentHandler>(); if (handler == null) { handler = transform.root.gameObject.AddComponent <EquipmentHandler>(); } handler.m_VisibleItems = m_VisibleItems; Destroy(this); return; } this.m_EquipmentContainer = WidgetUtility.Find <ItemContainer>(this.m_WindowName); if (this.m_EquipmentContainer != null) { for (int i = 0; i < this.m_VisibleItems.Count; i++) { this.m_VisibleItems[i].enabled = false; } this.m_EquipmentContainer.OnAddItem += OnAddItem; this.m_EquipmentContainer.OnRemoveItem += OnRemoveItem; UpdateEquipment(); if (InventoryManager.current != null) { InventoryManager.current.onSceneLoaded.AddListener(UpdateEquipment); } } }
// 将锚点设置到相对于父节点的中心,并且大小不改变 protected void setToNearParentCenterFixedSize() { Vector3[] sides = null; UIRect parentRect = WidgetUtility.findParentRect(gameObject); if (parentRect == null) { sides = getSides(null); } else { sides = getSides(parentRect.gameObject); } for (int i = 0; i < 4; ++i) { mAnchorPoint[i].setRelative(0.0f); if (i == 0 || i == 2) { mAnchorPoint[i].setAbsolute(sides[i].x); } else { mAnchorPoint[i].setAbsolute(sides[i].y); } } }
// 本地坐标系下的的各条边 protected Vector3[] getSides(GameObject parent) { // 如果自身带有旋转,则需要还原自身的变换 Vector3[] sides = new Vector3[4] { Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero }; UIRect thisRect = WidgetUtility.getGameObjectRect(gameObject); Vector3[] worldCorners = thisRect.worldCorners; Vector3[] localCorners = new Vector3[4]; for (int i = 0; i < 4; ++i) { if (parent != null) { localCorners[i] = parent.transform.InverseTransformPoint(worldCorners[i]); } else { localCorners[i] = worldCorners[i]; } } for (int i = 0; i < 4; ++i) { sides[i] = (localCorners[i] + localCorners[(i + 1) % 4]) / 2; } return(sides); }
// WidgetUtility //---------------------------------------------------------------------------------------------------------------------------------------- public static bool isNGUI(GameObject go) { #if MAKE_CS_DLL return((bool)callStatic(UTILITY_WIDGET, "isNGUI", new object[] { go })); #else return(WidgetUtility.isNGUI(go)); #endif }
public static void setNGUIWidgetSize(UIWidget widget, Vector2 size) { #if MAKE_CS_DLL callStatic(UTILITY_WIDGET, "setNGUIWidgetSize", new object[] { widget, size }); #else WidgetUtility.setNGUIWidgetSize(widget, size); #endif }
public static void setUGUIRectSize(RectTransform rectTransform, Vector2 size, bool adjustFont) { #if MAKE_CS_DLL callStatic(UTILITY_WIDGET, "setUGUIRectSize", new object[] { rectTransform, size, adjustFont }); #else WidgetUtility.setUGUIRectSize(rectTransform, size, adjustFont); #endif }
public static UIRect findNGUIParentRect(GameObject obj) { #if MAKE_CS_DLL return((UIRect)callStatic(UTILITY_WIDGET, "findNGUIParentRect", new object[] { obj })); #else return(WidgetUtility.findNGUIParentRect(obj)); #endif }
// 父节点在父节点坐标系下的各条边 public static void getParentSides(GameObject parent, Vector3[] sides) { #if MAKE_CS_DLL callStatic(UTILITY_WIDGET, "getParentSides", new object[] { parent, sides }); #else WidgetUtility.getParentSides(parent, sides); #endif }
public static Vector2 getUGUIRectSize(RectTransform rect) { #if MAKE_CS_DLL return((Vector2)callStatic(UTILITY_WIDGET, "getUGUIRectSize", new object[] { rect })); #else return(WidgetUtility.getUGUIRectSize(rect)); #endif }
public static void cornerToSide(Vector3[] corners, Vector3[] sides) { #if MAKE_CS_DLL callStatic(UTILITY_WIDGET, "cornerToSide", new object[] { corners, sides }); #else WidgetUtility.cornerToSide(corners, sides); #endif }
public static bool whetherGameObjectInScreen(Vector3 worldPos) { Vector3 screenPos = worldPosToScreenPos(worldPos); Vector2 rootSize = WidgetUtility.getRootSize(); return(screenPos.z >= 0.0f && (screenPos.x > 0 && screenPos.x < rootSize.x) && (screenPos.y > 0 && screenPos.y < rootSize.y)); }
private void Start() { this.m_EquipmentContainer = WidgetUtility.Find <ItemContainer>(this.m_WindowName); if (this.m_EquipmentContainer != null) { this.m_EquipmentContainer.OnAddItem += OnAddItem; this.m_EquipmentContainer.OnRemoveItem += OnRemoveItem; UpdateEquipment(); } }
public override ActionStatus OnUpdate() { Notification widget = WidgetUtility.Find <Notification>(this.m_WidgetName); if (widget == null) { Debug.LogWarning("Missing notification widget " + this.m_WidgetName + " in scene!"); return(ActionStatus.Failure); } return(widget.AddItem(this.m_Notification)?ActionStatus.Success:ActionStatus.Failure); }
public override void OnStart() { this.m_Time = 0f; this.m_Widget = WidgetUtility.Find <Progressbar>(this.m_WidgetName); if (this.m_Widget == null) { Debug.LogWarning("Missing progressbar widget " + this.m_WidgetName + " in scene!"); return; } this.m_Widget.Show(); }
//------------------------------------------------------------------------------------------------------------ protected override void setValue(float value) { txUIObject obj = mComponentOwner as txUIObject; // 因为NGUI中透明度小于0.001时认为是将窗口隐藏,会重新构建网格顶点,所以此处最低为0.002 if (WidgetUtility.isNGUI(obj.getObject())) { clampMin(ref value, 0.002f); } obj.setAlpha(value, false); }
public override void OnStart() { this.m_DialogBox = WidgetUtility.Find <DialogBox>(this.m_WidgetName); if (this.m_DialogBox == null) { Debug.LogWarning("Missing dialog box widget " + this.m_WidgetName + " in scene!"); return; } this.m_DialogBox.RegisterListener("OnClose", OnClose); this.m_Status = ActionStatus.Running; this.m_DialogBox.Show(this.m_Title, this.m_Text, this.m_Icon, OnResponse, "Yes", "No"); }
public override ActionStatus OnUpdate() { UIWidget widget = WidgetUtility.Find <UIWidget>(this.m_WidgetName); if (widget == null) { Debug.LogWarning("Missing notification widget " + this.m_WidgetName + " in scene!"); return(ActionStatus.Failure); } widget.Close(); return(ActionStatus.Success); }
//------------------------------------------------------------------------------------------------------------ protected override void applyTrembling(float value) { txUIObject obj = mComponentOwner as txUIObject; float newAlpha = lerpSimple(mStartAlpha, mTargetAlpha, value); // 因为NGUI中透明度小于0.001时认为是将窗口隐藏,会重新构建网格顶点,所以此处最低为0.002 if (WidgetUtility.isNGUI(obj.getObject())) { clampMin(ref newAlpha, 0.002f); } obj.setAlpha(newAlpha, false); }
private IEnumerator CraftItem(Item item) { this.m_ProgressDuration = item.CraftingDuration; this.m_ProgressInitTime = Time.time; yield return(new WaitForSeconds(item.CraftingDuration)); if (item.UseCraftingSkill) { ItemContainer skills = WidgetUtility.Find <ItemContainer>(item.SkillWindow); Skill skill = (Skill)skills.GetItems(item.CraftingSkill.Id).FirstOrDefault(); if (skill == null) { Debug.LogWarning("Skill not found in " + item.SkillWindow + "."); } if (!skill.CheckSkill()) { InventoryManager.Notifications.failedToCraft.Show(item.DisplayName); if (item.RemoveIngredientsWhenFailed) { for (int i = 0; i < item.ingredients.Count; i++) { this.m_RequiredIngredientsContainer.RemoveItem(item.ingredients[i].item, item.ingredients[i].amount); } } yield break; } } Item craftedItem = Instantiate(item); craftedItem.Stack = 1; craftedItem.CraftingModifier.Modify(craftedItem); if (this.m_ResultStorageContainer.StackOrAdd(craftedItem)) { for (int i = 0; i < item.ingredients.Count; i++) { this.m_RequiredIngredientsContainer.RemoveItem(item.ingredients[i].item, item.ingredients[i].amount); } InventoryManager.Notifications.craftedItem.Show(UnityTools.ColorString(craftedItem.Name, craftedItem.Rarity.Color)); ExecuteEvent <ITriggerCraftItem>(Execute, craftedItem); } else { InventoryManager.Notifications.containerFull.Show(this.m_ResultStorageContainer.Name); ExecuteEvent <ITriggerFailedToCraftItem>(Execute, item, FailureCause.ContainerFull); StopCrafting(item); } }
public void init(int renderOrder) { mScript = mLayoutManager.createScript(this); int count = mLayoutScriptCallback.Count; for (int i = 0; i < count; ++i) { mLayoutScriptCallback[i].Invoke(mScript, true); } if (mScript == null) { logError("can not create layout script! id : " + mID); return; } myUIObject parent = mIsScene ? null : mLayoutManager.getUIRoot(); // 初始化布局脚本 mScript.newObject(out mRoot, parent, mName); if (mRoot != null) { // 去除自带的锚点 // 在unity2020中,不知道为什么实例化以后的RectTransform的大小会自动变为窗口大小,为了适配计算正确,这里需要重置一次 RectTransform rectTransform = mRoot.getRectTransform(); Vector3 size = WidgetUtility.getRectSize(rectTransform); rectTransform.anchorMin = Vector2.one * 0.5f; rectTransform.anchorMax = Vector2.one * 0.5f; WidgetUtility.setRectSize(rectTransform, new Vector2(GameDefine.STANDARD_WIDTH, GameDefine.STANDARD_HEIGHT), false); } mRoot.setDestroyImmediately(true); mDefaultLayer = mRoot.getObject().layer; mScript.setRoot(mRoot); mScript.assignWindow(); // assignWindow后设置布局的渲染顺序,这样可以在此处刷新所有窗口的深度 setRenderOrder(renderOrder); // 布局实例化完成,初始化之前,需要调用自适应组件的更新 if (mLayoutManager.isUseAnchor()) { applyAnchor(mRoot.getObject(), true, this); } mAnchorApplied = true; mScript.init(); mScriptInited = true; // 加载完布局后强制隐藏 setVisibleForce(false); #if UNITY_EDITOR mRoot.getUnityComponent <LayoutDebug>().setLayout(this); #endif }
protected void updateRect(bool force = false) { if (!force && !mDirty) { return; } mDirty = false; float width = 0.0f; float height = 0.0f; Vector3 pos = Vector3.zero; UIRect parentRect = WidgetUtility.findParentRect(gameObject); if (parentRect != null) { GameObject parent = parentRect.gameObject; mParentSides = WidgetUtility.getParentSides(parent); float thisLeft = mAnchorPoint[0].mRelative * mParentSides[2].x + mAnchorPoint[0].mAbsolute; float thisRight = mAnchorPoint[2].mRelative * mParentSides[2].x + mAnchorPoint[2].mAbsolute; float thisTop = mAnchorPoint[1].mRelative * mParentSides[1].y + mAnchorPoint[1].mAbsolute; float thisBottom = mAnchorPoint[3].mRelative * mParentSides[1].y + mAnchorPoint[3].mAbsolute; width = thisRight - thisLeft; height = thisTop - thisBottom; pos.x = (thisRight + thisLeft) / 2.0f; pos.y = (thisTop + thisBottom) / 2.0f; } else { width = mAnchorPoint[2].mAbsolute - mAnchorPoint[0].mAbsolute; height = mAnchorPoint[1].mAbsolute - mAnchorPoint[3].mAbsolute; } if (width < 0) { UnityUtility.logError("width error in anchor!"); } if (height < 0) { UnityUtility.logError("height error in anchor!"); } UIWidget thisWidget = WidgetUtility.getGameObjectWidget(gameObject); // 没有widget则是panel,panel是没有宽高的 if (thisWidget != null) { thisWidget.width = (int)(width + 0.5f); thisWidget.height = (int)(height + 0.5f); } Transform thisTrans = gameObject.transform; thisTrans.localPosition = pos; }
private ActionStatus PickupItems() { if (this.m_ItemCollection.Count == 0) { InventoryManager.Notifications.empty.Show(gameObject.name.Replace("(Clone)", "").ToLower()); return(ActionStatus.Failure); } ItemContainer[] windows = WidgetUtility.FindAll <ItemContainer>(this.m_WindowName); List <Item> items = new List <Item>(); if (this.m_Amount < 0) { items.AddRange(this.m_ItemCollection); } else { for (int i = 0; i < this.m_Amount; i++) { Item item = this.m_ItemCollection[Random.Range(0, this.m_ItemCollection.Count)]; items.Add(item); } } for (int i = 0; i < items.Count; i++) { Item item = items[i]; if (windows.Length > 0) { for (int j = 0; j < windows.Length; j++) { ItemContainer current = windows[j]; if (current.StackOrAdd(item)) { this.m_ItemCollection.Remove(item); break; } } } else { //Drop items to ground DropItem(item); this.m_ItemCollection.Remove(item); } } return(ActionStatus.Success); }
public void PrintPrefabInfos() { var objs = WidgetUtility.LoadAllGameObject(); if (objs != null) { foreach (var item in objs) { Debug.Log(item.name); } } else { } }
protected override void Start() { base.Start(); this.m_ResultStorageContainer = WidgetUtility.Find <ItemContainer>(this.m_StorageWindow); this.m_RequiredIngredientsContainer = WidgetUtility.Find <ItemContainer>(this.m_IngredientsWindow); this.m_Progressbar = WidgetUtility.Find <Progressbar>(this.m_CraftingProgressbar); ItemContainer container = GetComponent <ItemContainer>(); if (container != null) { container.RegisterListener("OnShow", (CallbackEventData ev) => { InUse = true; }); container.RegisterListener("OnClose", (CallbackEventData ev) => { InUse = false; }); } }
//------------------------------------------------------------------------------------------------------------------------------ protected static void roundRectTransformToInt(RectTransform rectTransform) { if (rectTransform == null) { return; } rectTransform.localPosition = round(rectTransform.localPosition); WidgetUtility.setRectSize(rectTransform, round(WidgetUtility.getRectSize(rectTransform)), false); int childCount = rectTransform.childCount; for (int i = 0; i < childCount; ++i) { roundRectTransformToInt(rectTransform.GetChild(i) as RectTransform); } }