Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHOURUIH committed Oct 19, 2018
1 parent be26b93 commit f4c309e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 21 deletions.
112 changes: 96 additions & 16 deletions Assets/Editor/AutoAnchor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

public class AnchorMenu
{
public const string mAutoAnchorMenuName = "Auto Anchor";
[MenuItem(mAutoAnchorMenuName + "/Advance Anchor")]
public static void advanceAnchor()
public const string mAutoAnchorMenuName = "Auto Anchor/";
public const string mPaddingAnchorMenuName = "Padding Anchor/";
public const string mScaleAnchorMenuName = "Scale Anchor/";
[MenuItem(mAutoAnchorMenuName + mPaddingAnchorMenuName + "AddAnchor")]
public static void addPaddingAnchor()
{
if (Selection.gameObjects.Length <= 0)
{
Expand All @@ -30,11 +32,34 @@ public static void advanceAnchor()
}
for (int i = 0; i < count; ++i)
{
addAdvanceAnchor(Selection.gameObjects[i]);
addPaddingAnchor(Selection.gameObjects[i]);
}
}
[MenuItem(mAutoAnchorMenuName + "/Scale Anchor")]
public static void scaleAnchor()
[MenuItem(mAutoAnchorMenuName + mPaddingAnchorMenuName + "RemoveAnchor")]
public static void removePaddingAnchor()
{
if (Selection.gameObjects.Length <= 0)
{
return;
}
// 所选择的物体必须在同一个父节点下
Transform parent = Selection.transforms[0].parent;
int count = Selection.gameObjects.Length;
for (int i = 1; i < count; ++i)
{
if (parent != Selection.transforms[i].parent)
{
UnityUtility.logError("objects must have the same parent!");
return;
}
}
for (int i = 0; i < count; ++i)
{
removePaddingAnchor(Selection.gameObjects[i]);
}
}
[MenuItem(mAutoAnchorMenuName + mScaleAnchorMenuName + "AddAnchor")]
public static void addScaleAnchor()
{
if (Selection.gameObjects.Length <= 0)
{
Expand All @@ -56,7 +81,30 @@ public static void scaleAnchor()
addScaleAnchor(Selection.gameObjects[i]);
}
}
[MenuItem(mAutoAnchorMenuName + "/ScaleAnchor/AutoRelativePos")]
[MenuItem(mAutoAnchorMenuName + mScaleAnchorMenuName + "RemoveAnchor")]
public static void removeScaleAnchor()
{
if (Selection.gameObjects.Length <= 0)
{
return;
}
// 所选择的物体必须在同一个父节点下
Transform parent = Selection.transforms[0].parent;
int count = Selection.gameObjects.Length;
for (int i = 1; i < count; ++i)
{
if (parent != Selection.transforms[i].parent)
{
UnityUtility.logError("objects must have the same parent!");
return;
}
}
for (int i = 0; i < count; ++i)
{
removeScaleAnchor(Selection.gameObjects[i]);
}
}
[MenuItem(mAutoAnchorMenuName + mScaleAnchorMenuName + "AutoRelativePos")]
static void Calculation()
{
if (Selection.activeGameObject == null)
Expand All @@ -71,16 +119,16 @@ static void Calculation()
var obj = Selection.activeGameObject;
Vector2 thisPos = obj.transform.localPosition;
// 获取父物体的属性
UIRect parentRect = CustomAnchor.findParentRect(obj);
Vector2 parentSize = CustomAnchor.getRectSize(parentRect);
UIRect parentRect = WidgetUtility.findParentRect(obj);
Vector2 parentSize = WidgetUtility.getRectSize(parentRect);
// 计算
anchor.mHorizontalRelativePos = thisPos.x / parentSize.x * 2;
anchor.mVerticalRelativePos = thisPos.y / parentSize.y * 2;
// 设置成自定义方式
anchor.mPadding = PADDING_STYLE.PS_CUSTOM_VALUE;
}
// 清除 PADDING_STYLE 和数值
[MenuItem(mAutoAnchorMenuName + "/ScaleAnchor/DefaultRelativePos")]
[MenuItem(mAutoAnchorMenuName + mScaleAnchorMenuName + "DefaultRelativePos")]
static void ClaerCalculation()
{
if(Selection.activeGameObject == null)
Expand All @@ -93,20 +141,38 @@ static void ClaerCalculation()
Anchor.mPadding = PADDING_STYLE.PS_NONE;
}
//-------------------------------------------------------------------------------------------------------------------
public static void addAdvanceAnchor(GameObject obj)
public static void addPaddingAnchor(GameObject obj)
{
// 先设置自己的Anchor
UIWidget widget = CustomAnchor.getGameObjectWidget(obj);
if(widget != null)
if (obj.GetComponent<PaddingAnchor>() == null)
{
CustomAnchor anchor = obj.AddComponent<CustomAnchor>();
anchor._mAnchorMode = ANCHOR_MODE.AM_NEAR_PARENT_SIDE;
// 只要有Rect就可以添加该组件,panel也可以添加
UIRect rect = WidgetUtility.getGameObjectRect(obj);
if(rect != null)
{
PaddingAnchor anchor = obj.AddComponent<PaddingAnchor>();
anchor.setAnchorMode(ANCHOR_MODE.AM_NEAR_PARENT_SIDE);
}
}
// 再设置子节点的Anchor
int childCount = obj.transform.childCount;
for(int i = 0; i < childCount; ++i)
{
addAdvanceAnchor(obj.transform.GetChild(i).gameObject);
addPaddingAnchor(obj.transform.GetChild(i).gameObject);
}
}
public static void removePaddingAnchor(GameObject obj)
{
// 先销毁自己的Anchor
if (obj.GetComponent<PaddingAnchor>() != null)
{
UnityUtility.destroyGameObject(obj.GetComponent<PaddingAnchor>(), true);
}
// 再销毁子节点的Anchor
int childCount = obj.transform.childCount;
for (int i = 0; i < childCount; ++i)
{
removePaddingAnchor(obj.transform.GetChild(i).gameObject);
}
}
public static void addScaleAnchor(GameObject obj)
Expand All @@ -123,4 +189,18 @@ public static void addScaleAnchor(GameObject obj)
addScaleAnchor(obj.transform.GetChild(i).gameObject);
}
}
public static void removeScaleAnchor(GameObject obj)
{
// 先销毁自己的Anchor
if (obj.GetComponent<ScaleAnchor>() != null)
{
UnityUtility.destroyGameObject(obj.GetComponent<ScaleAnchor>(), true);
}
// 再销毁子节点的Anchor
int childCount = obj.transform.childCount;
for (int i = 0; i < childCount; ++i)
{
removeScaleAnchor(obj.transform.GetChild(i).gameObject);
}
}
}
10 changes: 5 additions & 5 deletions Assets/Scripts/Frame/Common/Utility/UnityUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ public static bool whetherGameObjectInScreen(Vector3 worldPos)
{
Vector3 screenPos = worldPosToScreenPos(worldPos);
return screenPos.z >= 0.0f &&
(screenPos.x > 0 && screenPos.x < Screen.currentResolution.width) &&
(screenPos.y > 0 && screenPos.y < Screen.currentResolution.height);
(screenPos.x > 0 && screenPos.x < UnityEngine.Screen.currentResolution.width) &&
(screenPos.y > 0 && screenPos.y < UnityEngine.Screen.currentResolution.height);
}
public static Vector2 screenPosToWindowPos(Vector2 screenPos, txUIObject parent, bool screenCenterOsZero = false, bool isNGUI = true)
{
Camera camera = mCameraManager.getUICamera().getCamera();
screenPos.x = screenPos.x / camera.pixelWidth * Screen.currentResolution.width;
screenPos.y = screenPos.y / camera.pixelHeight * Screen.currentResolution.height;
screenPos.x = screenPos.x / camera.pixelWidth * UnityEngine.Screen.currentResolution.width;
screenPos.y = screenPos.y / camera.pixelHeight * UnityEngine.Screen.currentResolution.height;
Vector3 parentWorldPosition = parent != null ? parent.getWorldPosition() : Vector3.zero;
txUIObject root = isNGUI ? mLayoutManager.getNGUIRoot() : mLayoutManager.getUGUIRoot();
Vector3 scale = root.getScale();
Expand All @@ -323,7 +323,7 @@ public static Vector2 screenPosToWindowPos(Vector2 screenPos, txUIObject parent,
Vector2 windowPos = new Vector2(pos.x / parentWorldScale.x, pos.y / parentWorldScale.y);
if(screenCenterOsZero)
{
windowPos -= new Vector2(Screen.currentResolution.width / 2.0f, Screen.currentResolution.height / 2.0f);
windowPos -= new Vector2(UnityEngine.Screen.currentResolution.width / 2.0f, UnityEngine.Screen.currentResolution.height / 2.0f);
}
return windowPos;
}
Expand Down

0 comments on commit f4c309e

Please sign in to comment.