Files
Main/Assets/GameAssets/Resources/GameUI/Common/Utils/UIUtility.cs

1650 lines
58 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System;
using Thousandto.Core.Base;
using UnityEngine;
using Thousandto.Cfg.Data;
using Thousandto.Core.Asset;
using Thousandto.Code.Logic;
using Thousandto.GameUI.Form;
using UnityEngine.Video;
using Thousandto.Code.Global;
using XLua;
using PathUtils = UnityEngine.Gonbest.MagicCube.PathUtils;
using CoroutinePool = UnityEngine.Gonbest.MagicCube.CoroutinePool;
using StringUtils = UnityEngine.Gonbest.MagicCube.StringUtils;
using UnityEngine.Playables;
using Thousandto.Code.Center;
namespace Thousandto.Plugins.Common
{
/// <summary>
/// UI数据操作简化
/// </summary>
public class UIUtility
{
#region UI空间 3D
// UI坐标变换 -> 获取屏幕 -> (相对于UI Center坐标)
private static Vector3? GetObjViewPortPosFromUICamera( GameObject uiObj, Camera uiCamera )
{
if( uiCamera != null && uiObj != null )
{
Vector3 objWorldPos = uiObj.transform.position;
return uiCamera.WorldToViewportPoint( objWorldPos );
}
return null;
}
// 屏幕坐标等比于UI相机ViewPort位置 -> 获取目标相机照射空间位置
private static Vector3? ViewPortPosToTagCameraViewWorldPos( Camera mainCamera, Vector3 viewPortPos, float z = 10.0f )
{
if( mainCamera == null )
{
return null;
}
viewPortPos.z = z;
Vector3 worldPos = mainCamera.ViewportToWorldPoint( viewPortPos );
return worldPos;
}
// 获取相对Center位置 UI坐标到目标相机世界坐标
public static bool GetGameObjectPositionFromUICameraToTagCamera( GameObject uiObj, Camera uiCamera, Camera mainCamera,
ref Vector3 getWorldPos, float zSet = 10.0f )
{
bool succ = false;
Vector3? viewPortPos = GetObjViewPortPosFromUICamera( uiObj, uiCamera );
if( viewPortPos.HasValue )
{
Vector3? worldPos = ViewPortPosToTagCameraViewWorldPos( mainCamera, (Vector3)viewPortPos, zSet );
if( worldPos.HasValue )
{
getWorldPos = (Vector3)worldPos;
succ = true;
}
}
return succ;
}
//根据世界坐标获取在地图UI上的坐标
public static Vector3 WorldPosToMapPos( float camYaw, Vector2 uiSize, Vector2 mapSize, Vector2 camPos, Vector3 worldPos )
{
Vector3 posInMini = new Vector2();
Quaternion rotateYaw = Quaternion.AngleAxis( -camYaw, Vector3.up );
Matrix4x4 viewMatrix = Matrix4x4.TRS( Vector3.zero, rotateYaw, Vector3.one );
Vector3 world_center_offset = new Vector3( worldPos.x - camPos.x, 0.0f, worldPos.z - camPos.y );
Vector3 camera_domain_offset = viewMatrix.MultiplyPoint( world_center_offset );
float scale_x = uiSize.x / mapSize.x;
float scale_z = uiSize.y / mapSize.y;
posInMini.x = camera_domain_offset.x * scale_x;
posInMini.y = camera_domain_offset.z * scale_z;
posInMini.z = 0.0f;
return posInMini;
}
//根据世界坐标获取在地图UI上的坐标
public static Vector3 WorldPosToMiniMapPos( float camYaw,
float uiSizeX, float uiSizeY,
float mapSizeX, float mapSizeY,
float camPosX, float camPosY,
float worldPosX, float worldPosY, float worldPosZ )
{
Vector3 posInMini = new Vector2();
Quaternion rotateYaw = Quaternion.AngleAxis( -camYaw, Vector3.up );
Matrix4x4 viewMatrix = Matrix4x4.TRS( Vector3.zero, rotateYaw, Vector3.one );
Vector3 world_center_offset = new Vector3( worldPosX - camPosX, 0.0f, worldPosZ - camPosY );
Vector3 camera_domain_offset = viewMatrix.MultiplyPoint( world_center_offset );
float scale_x = uiSizeX / mapSizeX;
float scale_z = uiSizeY / mapSizeY;
posInMini.x = camera_domain_offset.x * scale_x;
posInMini.y = camera_domain_offset.z * scale_z;
posInMini.z = 0.0f;
return posInMini;
}
//根据地图UI上的坐标获取在世界坐标
public static Vector3 MapPosToWorldPos( float camYaw, Vector2 uiSize, Vector2 mapSize, Vector2 camPos, Vector2 clickPos )
{
Vector3 worldPos = Vector3.zero;
Quaternion invRotateYaw = Quaternion.AngleAxis( camYaw, Vector3.up );
Matrix4x4 invViewMatrix = Matrix4x4.TRS( Vector3.zero, invRotateYaw, Vector3.one );
// current scale
float scale_x = uiSize.x / mapSize.x;
float scale_z = uiSize.y / mapSize.y;
// mini offset -> world offset
worldPos.x = clickPos.x / scale_x;
worldPos.z = clickPos.y / scale_z;
// camera_domain -> world_domain(based by camera center)
worldPos = invViewMatrix.MultiplyPoint( worldPos );
// normal world Axis
worldPos += new Vector3( camPos.x, 0, camPos.y );
return worldPos;
}
//根据地图UI上的坐标获取在世界坐标
public static Vector3 MiniMapPosToWorldPos( float camYaw,
float uiSizeX, float uiSizeY,
float mapSizeX, float mapSizeY,
float camPosX, float camPosY,
float clickPosX, float clickPosY, float clickPosZ )
{
Vector3 worldPos = Vector3.zero;
Quaternion invRotateYaw = Quaternion.AngleAxis( camYaw, Vector3.up );
Matrix4x4 invViewMatrix = Matrix4x4.TRS( Vector3.zero, invRotateYaw, Vector3.one );
// current scale
float scale_x = uiSizeX / mapSizeX;
float scale_z = uiSizeY / mapSizeY;
// mini offset -> world offset
worldPos.x = clickPosX / scale_x;
worldPos.z = clickPosY / scale_z;
// camera_domain -> world_domain(based by camera center)
worldPos = invViewMatrix.MultiplyPoint( worldPos );
// normal world Axis
worldPos += new Vector3( camPosX, 0, camPosY );
return worldPos;
}
public static void RotationToForward( Transform trans, float angle )
{
trans.localRotation = Quaternion.AngleAxis( angle, Vector3.forward );
}
#endregion
#region//处理GameObject名字的函数
//根据名字获取索引 :名字的格式: XXXX_01,XXXX_02 ,如果没有正确值的话,返回-1
public static int GetIndexByName( string goName )
{
int result = -1;
var arr = goName.Split( new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries );
if( arr.Length == 2 )
{
int.TryParse( arr[ 1 ], out result );
}
return result;
}
//通过索引获取名字:prefixName_01
public static string GetNameByIndex( string prefixName, int index )
{
return string.Format( "{0}_{0:2D}", prefixName, index );
}
#endregion
#region //设置sprite
/// <summary>
/// 设计材料icon
/// </summary>
/// <param name="id"></param>
/// <param name="sprite"></param>
public static void SetMateralImgById( int id, UISprite sprite, UISprite qualityIcon = null )
{
//ItemModel model = GameCenter.ItemContianerSystem.GetItemByID<ItemModel>(ContainerType.ITEM_LOCATION_BAG, id);
DeclareItem item = DeclareItem.Get( id );
if( item == null )
{
//UnityEngine.Debug.LogError( "设置icon失败 id=" + id );
return;
}
SetIcon( item.Icon, sprite );
if( qualityIcon != null )
{
qualityIcon.color = ItemBase.GetQualityColor( item.Color );
}
}
/// <summary>
/// 设置装备icon
/// </summary>
/// <param name="id"></param>
/// <param name="sprite"></param>
public static DeclareEquip SetEquipImgById( int id, UISprite sprite, UISprite qualityIcon = null )
{
//ItemModel model = GameCenter.ItemContianerSystem.GetItemByUID<ItemModel>((ulong)id);
DeclareEquip item = DeclareEquip.Get( id );
if( item == null )
{
//UnityEngine.Debug.LogError( "设置icon失败 id=" + id );
return null;
}
SetIcon( item.Icon, sprite );
if( qualityIcon != null )
{
qualityIcon.color = ItemBase.GetQualityColor( item.Quality );
}
return item;
}
/// <summary>
/// 设置icon图标
/// </summary>
/// <param name="iconId"></param>
/// <param name="sprite"></param>
public static void SetIcon( int iconId, UISprite sprite )
{
var iconbase = sprite.transform.RequireComponent<UIIconBase>();
iconbase.UpdateIcon( iconId );
}
/// <summary>
/// 设置技能图标
/// </summary>
/// <param name="id"></param>
/// <param name="sprite"></param>
public static void SetSkillImgById( int id, UISprite sprite )
{
//ItemModel model = GameCenter.ItemContianerSystem.GetItemByID<ItemModel>(ContainerType.ITEM_LOCATION_BAG, id);
DeclareSkill item = DeclareSkill.Get( id );
if( item == null )
{
//UnityEngine.Debug.LogError( "技能找不到: " + id );
return;
}
SetIcon( item.Icon, sprite );
// UIPoolAssetsLoader.LoadAtlasPrefabAsyn(atlasName, atlasTrans =>
// {
// if (atlasTrans != null)
// {
// var itemAtlas = atlasTrans.GetComponent<UIAtlas>();
// if (null != itemAtlas)
// {
// sprite.atlas = itemAtlas;
// sprite.spriteName = spriteName;
// }
// }
// });
}
//为一个button的各种状态只有一个spritename
public static void SetOneSpriteWithButton( UIButton btn, string spriteName )
{
if( btn != null )
{
btn.normalSprite = spriteName;
btn.hoverSprite = spriteName;
btn.pressedSprite = spriteName;
btn.disabledSprite = spriteName;
}
}
//设置Tab按钮状态的设置
public static void SetTabButtonState( UIButton btn, UILabel label, bool isSelected )
{
if( isSelected )
{
btn.tweenTarget.transform.localScale = new Vector3( 1f, 1.1f, 1f );
//btn.normalSprite = "mainbutton2";
//btn.hoverSprite = "mainbutton2";
//btn.pressedSprite = "mainbutton2";
label.color = Color.white;
label.effectColor = new Color( 147 * 0.00392f, 32 * 0.00392f, 32 * 0.00392f );
}
else
{
btn.tweenTarget.transform.localScale = new Vector3( 1f, 1f, 1f );
//btn.normalSprite = "mainbutton1";
//btn.hoverSprite = "mainbutton1";
//btn.pressedSprite = "mainbutton1";
label.color = new Color( 197 * 0.00392f, 150 * 0.00392f, 78 * 0.00392f );
label.effectColor = Color.black;
}
}
//声音播放回调
public static void PlayUISoundFX( String soundName )
{
AudioPlayer.PlayUI( soundName );
}
//设置按钮状态
public static void SetBtnState( Transform trans, bool b )
{
if( trans )
{
UIButton btn = trans.RequireComponent<UIButton>();
if( btn != null )
btn.isEnabled = b;
}
}
//设置按钮响应时间
public static void SetBtnCall( Transform trans, EventDelegate.Callback func )
{
if( trans )
{
UIButton btn = trans.RequireComponent<UIButton>();
if( btn != null )
{
btn.onClick.Clear();
btn.onClick.Add( new EventDelegate( func ) );
}
}
}
//设置图片
public static void SetUISprite( Transform trans, string name )
{
if( trans )
{
UISprite spr = trans.RequireComponent<UISprite>();
if( spr != null )
{
spr.spriteName = name;
}
}
}
//设置slider
public static void SetUISlider( Transform trans, float value )
{
if( trans )
{
UISlider slider = trans.RequireComponent<UISlider>();
if( slider != null )
{
slider.value = value;
}
}
}
//设置文字颜色
public static void SetLabelColor( UILabel text, Color color )
{
if( text != null )
{
text.color = color;
}
}
public static void SetAllChildColorGray( Transform trs, bool isgray )
{
NGUITools.SetButtonGrayAndNotOnClick( trs, isgray );
}
#endregion
//克隆
public static GameObject Clone( GameObject go , bool isReset = false)
{
GameObject newGameObject = (GameObject)( GameObject.Instantiate( go ) );
newGameObject.name = go.name;
var tf = newGameObject.transform;
tf.parent = go.transform.parent;
if (isReset)
{
UnityUtils.Reset(tf);
}
else
{
tf.localRotation = go.transform.localRotation;
tf.localScale = go.transform.localScale;
tf.localPosition = go.transform.localPosition;
}
return newGameObject;
}
//克隆
public static GameObject Clone( GameObject go, Transform parentTrans, bool isReset = false)
{
GameObject newGameObject = (GameObject)( GameObject.Instantiate( go ) );
var tf = newGameObject.transform;
tf.parent = parentTrans;
if (isReset)
{
UnityUtils.Reset(tf);
}
else
{
tf.localRotation = go.transform.localRotation;
tf.localScale = go.transform.localScale;
tf.localPosition = go.transform.localPosition;
}
return newGameObject;
}
/// <summary>
/// 泰国奇葩的语言显示竖着的文字需要把label倒过来
/// </summary>
/// <param name="label"></param>
public static void SetTHLabel( UILabel label )
{
/*
if (LanguageSystem.Lang == LanguageConstDefine.TH)
{
if (label.transform.localEulerAngles.z == 0)
{
label.transform.localEulerAngles = new Vector3(0, 0, 90);
int width = label.width;
int height = label.height;
label.width = height;
label.height = width;
//label.SetDimensions(height, width);
label.overflowMethod = UILabel.Overflow.ShrinkContent;
label.alignment = NGUIText.Alignment.Center;
}
}
else
{
if (label.transform.localEulerAngles.z == 90)
{
label.transform.localEulerAngles = Vector3.zero;
}
}
* */
}
//获取控件size.x
public static float GetSizeX( Transform tf )
{
UIWidget uiWidget = tf.GetComponent<UIWidget>();
if( uiWidget != null )
{
return uiWidget.localSize.x;
}
return 0f;
}
//获取控件size.y
public static float GetSizeY( Transform tf )
{
UIWidget uiWidget = tf.GetComponent<UIWidget>();
if( uiWidget != null )
{
return uiWidget.localSize.y;
}
return 0f;
}
//获取控件size
public static Vector2 GetSize( Transform tf )
{
UIWidget uiWidget = tf.GetComponent<UIWidget>();
if( uiWidget != null )
{
return uiWidget.localSize;
}
return Vector2.zero;
}
//增加 UIPanel 组件
public static UIPanel RequireUIPanel( Transform tf )
{
var _componet = tf.GetComponent<UIPanel>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIPanel>();
}
return _componet;
}
//增加 UILabel 组件
public static UILabel RequireUILabel( Transform tf )
{
var _componet = tf.GetComponent<UILabel>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UILabel>();
}
return _componet;
}
//增加 UISprite 组件
public static UISprite RequireUISprite( Transform tf )
{
var _componet = tf.GetComponent<UISprite>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UISprite>();
}
return _componet;
}
//增加 UIButton 组件
public static UIButton RequireUIButton( Transform tf )
{
var _componet = tf.GetComponent<UIButton>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIButton>();
}
return _componet;
}
//增加 UITexture 组件
public static UITexture RequireUITexture( Transform tf )
{
var _componet = tf.GetComponent<UITexture>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UITexture>();
}
return _componet;
}
//增加 UISlider 组件
public static UISlider RequireUISlider( Transform tf )
{
var _componet = tf.GetComponent<UISlider>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UISlider>();
}
return _componet;
}
//增加 UIScrollView 组件
public static UIScrollView RequireUIScrollView( Transform tf )
{
var _componet = tf.GetComponent<UIScrollView>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIScrollView>();
}
return _componet;
}
//增加 UITable 组件
public static UITable RequireUITable( Transform tf )
{
var _componet = tf.GetComponent<UITable>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UITable>();
}
return _componet;
}
//增加 UIGrid 组件
public static UIGrid RequireUIGrid( Transform tf )
{
var _componet = tf.GetComponent<UIGrid>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIGrid>();
}
return _componet;
}
//增加 UIItem 组件
public static UIItem RequireUIItem( Transform tf )
{
var _componet = tf.GetComponent<UIItem>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIItem>();
}
return _componet;
}
//增加 UIListMenu 组件
public static UIListMenu RequireUIListMenu( Transform tf )
{
var _componet = tf.GetComponent<UIListMenu>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIListMenu>();
}
return _componet;
}
//增加 UIRoleSkinCompoent 组件
public static UIRoleSkinCompoent RequireUIRoleSkinCompoent( Transform tf )
{
var _componet = tf.GetComponent<UIRoleSkinCompoent>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIRoleSkinCompoent>();
}
return _componet;
}
//增加 UIPlayerSkinCompoent 组件
public static UIPlayerSkinCompoent RequireUIPlayerSkinCompoent( Transform tf )
{
var _componet = tf.GetComponent<UIPlayerSkinCompoent>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIPlayerSkinCompoent>();
}
return _componet;
}
//增加 UIVfxSkinCompoent 组件
public static UIVfxSkinCompoent RequireUIVfxSkinCompoent( Transform tf )
{
var _componet = tf.GetComponent<UIVfxSkinCompoent>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIVfxSkinCompoent>();
}
return _componet;
}
//增加 UIIcon 组件
public static UIIcon RequireUIIcon( Transform tf )
{
var _componet = tf.GetComponent<UIIcon>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIIcon>();
}
return _componet;
}
//增加 UIIconBase 组件
public static UIIconBase RequireUIIconBase( Transform tf )
{
var _componet = tf.GetComponent<UIIconBase>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIIconBase>();
}
return _componet;
}
//增加 UIToggle 组件
public static UIToggle RequireUIToggle( Transform tf )
{
var _componet = tf.GetComponent<UIToggle>();
if( _componet == null )
{
return tf.gameObject.AddComponent<UIToggle>();
}
return _componet;
}
//增加 SpringPanel 组件
public static SpringPanel RequireSpringPanel( Transform tf )
{
var _componet = tf.GetComponent<SpringPanel>();
if( _componet == null )
{
return tf.gameObject.AddComponent<SpringPanel>();
}
return _componet;
}
//增加 UISpriteAnimation 组件
public static UISpriteAnimation RequireUISpriteAnimation(Transform tf)
{
var _componet = tf.GetComponent<UISpriteAnimation>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UISpriteAnimation>();
}
return _componet;
}
//增加 UIPolygonScript 组件
public static UIPolygonScript RequireUIPolygonScript(Transform tf)
{
var _componet = tf.GetComponent<UIPolygonScript>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UIPolygonScript>();
}
return _componet;
}
//增加 UIPlayerLevelLabel 组件
public static UIPlayerLevelLabel RequireUIPlayerLevelLabel(Transform tf)
{
var _componet = tf.GetComponent<UIPlayerLevelLabel>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UIPlayerLevelLabel>();
}
return _componet;
}
//增加 NatureVfxEffect 组件
public static NatureVfxEffect RequireNatureVfxEffect(Transform tf)
{
var _componet = tf.GetComponent<NatureVfxEffect>();
if (_componet == null)
{
return tf.gameObject.AddComponent<NatureVfxEffect>();
}
return _componet;
}
//增加 UILoopScrollViewBase 组件
public static UILoopScrollViewBase RequireUILoopScrollViewBase(Transform tf)
{
var _componet = tf.GetComponent<UILoopScrollViewBase>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UILoopScrollViewBase>();
}
return _componet;
}
//增加 UIPlayerBagItem 组件
public static UIPlayerBagItem RequireUIPlayerBagItem(Transform tf)
{
var _componet = tf.GetComponent<UIPlayerBagItem>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UIPlayerBagItem>();
}
return _componet;
}
// 增加 UISelectEquipItem 组件
public static UISelectEquipItem RequireUISelectEquipItem( Transform t )
{
var _comp = t.GetComponent<UISelectEquipItem>();
if( _comp == null )
{
return t.gameObject.AddComponent<UISelectEquipItem>();
}
return _comp;
}
// 增加 LoopMoveSceneScript 组件
public static LoopMoveSceneScript RequireLoopMoveSceneScript( Transform t )
{
var _comp = t.GetComponent<LoopMoveSceneScript>();
if( _comp == null )
{
return t.gameObject.AddComponent<LoopMoveSceneScript>();
}
return _comp;
}
// 增加 UIVideoTexture 组件
public static UIVideoTexture RequireUIVideoTexture( Transform t )
{
var _comp = t.GetComponent<UIVideoTexture>();
if( _comp == null )
{
return t.gameObject.AddComponent<UIVideoTexture>();
}
return _comp;
}
// 增加 DragScrollView 组件
public static UIDragScrollView RequireDragScrollView( Transform t )
{
var _comp = t.GetComponent<UIDragScrollView>();
if( _comp == null )
{
return t.gameObject.AddComponent<UIDragScrollView>();
}
return _comp;
}
public static TweenPosition RequireTweenPosition( Transform t )
{
var _comp = t.gameObject.RequireComponent<TweenPosition>();
return _comp;
}
//注册动画脚本
public static AnimListBaseScript RequireAnimListBaseScript( Transform t )
{
var _comp = t.GetComponent<AnimListBaseScript>();
if( _comp == null )
{
return t.gameObject.AddComponent<AnimListBaseScript>();
}
return _comp;
}
//注册animator脚本
public static Animator RequireAnimator( Transform t )
{
var _comp = t.GetComponent<Animator>();
if( _comp == null )
{
return t.gameObject.AddComponent<Animator>();
}
return _comp;
}
//获取ui阴影
public static UIShandowPlane RequireUIShandowPlane(Transform t)
{
var _comp = t.GetComponent<UIShandowPlane>();
if (_comp == null)
{
_comp = t.gameObject.AddComponent<UIShandowPlane>();
}
return _comp;
}
//获取摄像机
public static Camera RequireCamera(Transform t)
{
var _comp = t.GetComponent<Camera>();
if (_comp == null)
{
_comp = t.gameObject.AddComponent<Camera>();
}
return _comp;
}
//获取金币窗体
public static UIMoneyForm RequireUIMoneyForm(Transform t)
{
var _comp = t.GetComponent<UIMoneyForm>();
if (_comp == null)
{
_comp = t.gameObject.AddComponent<UIMoneyForm>();
}
return _comp;
}
//获取帧动画
public static UIFrameAnimation RequireUIFrameAnimation(Transform t)
{
var _comp = t.GetComponent<UIFrameAnimation>();
if (_comp == null)
{
_comp = t.gameObject.AddComponent<UIFrameAnimation>();
}
return _comp;
}
//lua端调用c#的string.Format函数
public static string Format( string original, params object[] str )
{
return string.Format( original, str );
}
public static string FormatLuatable( string original, object obj )
{
var _ret = string.Empty;
var _luaTable = obj as XLua.LuaTable;
if( _luaTable != null )
{
var _params = new object[ _luaTable.Length ];
for( int i = 0; i < _luaTable.Length; i++ )
{
var ret = string.Empty;
_luaTable.Get<int, string>( i + 1, out ret );
_params[ i ] = ret;
}
_ret = string.Format( original, _params );
}
return _ret;
}
//设置颜色
public static void SetColor( UILabel label, float r, float g, float b, float a )
{
label.color = new Color( r, g, b, a );
}
//设置颜色
public static void SetColor( UILabel label, string str )
{
Color color;
if( ColorUtility.TryParseHtmlString( str, out color ) )
{
label.color = color;
}
}
public static void SetEffectColor(UILabel label, float r, float g, float b, float a)
{
label.effectColor = new Color(r, g, b, a);
}
//根据品质设置颜色
public static void SetColorByQuality(UILabel label, int quality)
{
label.color = ItemBase.GetQualityColor(quality);
}
//设置颜色
public static void SetColor( UITexture texture, float r, float g, float b, float a )
{
texture.color = new Color( r, g, b, a );
}
//设置颜色
public static void SetColor( UITexture texture, string str )
{
Color color;
if( ColorUtility.TryParseHtmlString( str, out color ) )
{
texture.color = color;
}
}
//设置颜色
public static void SetColor( UISprite sprite, float r, float g, float b, float a )
{
sprite.color = new Color( r, g, b, a );
}
//设置颜色
public static void SetColor( UISprite sprite, string str )
{
Color color;
if( ColorUtility.TryParseHtmlString( str, out color ) )
{
sprite.color = color;
}
}
//设置sprite渐变色
public static void SetSprGenColor( UISprite sprite, int quality )
{
if( sprite != null )
{
switch(quality)
{
case QualityCode.White:
sprite.rtColor = Color.white;
sprite.rbColor = Color.white;
sprite.ltColor = Color.white;
sprite.lbColor = Color.white;
break;
case QualityCode.Green:
sprite.rtColor = new Color( 36 / 255.0f, 64 / 255.0f, 78 / 255.0f );
sprite.rbColor = new Color( 48 / 255.0f, 104 / 255.0f, 113 / 255.0f );
sprite.ltColor = new Color( 60 / 255.0f, 193 / 255.0f, 189 / 255.0f );
sprite.lbColor = new Color( 60 / 255.0f, 193 / 255.0f, 189 / 255.0f );
break;
case QualityCode.Blue:
sprite.rtColor = new Color( 36 / 255.0f, 49 / 255.0f, 78 / 255.0f );
sprite.rbColor = new Color( 48 / 255.0f, 74 / 255.0f, 112 / 255.0f );
sprite.ltColor = new Color( 19 / 255.0f, 95 / 255.0f, 178 / 255.0f );
sprite.lbColor = new Color( 128 / 255.0f, 182 / 255.0f, 1 );
break;
case QualityCode.Violet:
sprite.rtColor = new Color( 57 / 255.0f, 40 / 255.0f, 78 / 255.0f );
sprite.rbColor = new Color( 88 / 255.0f, 49 / 255.0f, 112 / 255.0f );
sprite.ltColor = new Color( 156 / 255.0f, 50 / 255.0f, 207 / 255.0f );
sprite.lbColor = new Color( 223 / 255.0f, 131 / 255.0f, 1.0f );
break;
case QualityCode.Orange:
sprite.rtColor = new Color( 71 / 255.0f, 54 / 255.0f, 54 / 255.0f );
sprite.rbColor = new Color( 117 / 255.0f, 78 / 255.0f, 68 / 255.0f );
sprite.ltColor = new Color( 202 / 255.0f, 96 / 255.0f, 49 / 255.0f );
sprite.lbColor = new Color( 244 / 255.0f, 179 / 255.0f, 146 / 255.0f );
break;
case QualityCode.Golden:
sprite.rtColor = new Color( 69 / 255.0f, 59 / 255.0f, 47 / 255.0f );
sprite.rbColor = new Color( 113 / 255.0f, 90 / 255.0f, 50 / 255.0f );
sprite.ltColor = new Color( 220 / 255.0f, 158 / 255.0f, 41 / 255.0f );
sprite.lbColor = new Color( 244 / 255.0f, 212 / 255.0f, 107 / 255.0f );
break;
case QualityCode.Red:
sprite.rtColor = new Color( 53 / 255.0f, 37 / 255.0f, 48 / 255.0f );
sprite.rbColor = new Color( 88 / 255.0f, 42 / 255.0f, 49 / 255.0f );
sprite.ltColor = new Color( 168 / 255.0f, 29 / 255.0f, 35 / 255.0f );
sprite.lbColor = new Color( 230 / 255.0f, 98 / 255.0f, 95 / 255.0f );
break;
case QualityCode.Pink:
sprite.rtColor = new Color( 96 / 255.0f, 37 / 255.0f, 78 / 255.0f );
sprite.rbColor = new Color( 147 / 255.0f, 52 / 255.0f, 120 / 255.0f );
sprite.ltColor = new Color( 230 / 255.0f, 14 / 255.0f, 179 / 255.0f );
sprite.lbColor = new Color( 251 / 255.0f, 155 / 255.0f, 227 / 255.0f );
break;
case QualityCode.DarkGolden:
sprite.rtColor = new Color( 46 / 255.0f, 39 / 255.0f, 48 / 255.0f );
sprite.rbColor = new Color( 75 / 255.0f, 48 / 255.0f, 50 / 255.0f );
sprite.ltColor = new Color( 155 / 255.0f, 63 / 255.0f, 32 / 255.0f );
sprite.lbColor = new Color( 235 / 255.0f, 156 / 255.0f, 97 / 255.0f );
break;
}
}
}
// 复制文本到剪贴板
public static void CopyToClipboard( string content )
{
var _msg = string.Empty;
if( !string.IsNullOrEmpty( content ) )
{
#if UNITY_EDITOR
GUIUtility.systemCopyBuffer = content;
#else
Code.Center.GameCenter.SDKSystem.CopyToClipboard( content );
#endif
_msg = Thousandto.Cfg.Data.DeclareMessageString.Get(Thousandto.Cfg.Data.DeclareMessageString.C_COPY_BORD_SUCC);
}
else
{
_msg = Thousandto.Cfg.Data.DeclareMessageString.Get(Thousandto.Cfg.Data.DeclareMessageString.C_COPY_BORD_ISEMPITY);
}
Code.Center.GameCenter.MsgPromptSystem.ShowPrompt( _msg );
}
public static int GetStringBytesLength( string str )
{
if( string.IsNullOrEmpty( str ) )
{
return 0;
}
else
{
return StringUtils.CalcStringLenByUCS(str);
}
}
public static string StringSubString( string str, int startIndex, int length = 0 )
{
var ret = string.Empty;
if( !string.IsNullOrEmpty( str ) )
{
return StringUtils.SubStringByUCS(str,startIndex,length);
}
return ret;
}
public static void SetGameObjectNameByNumber(GameObject go, int number)
{
go.name = number.ToString();
}
#region UILabel
public static void SetTextByMessageStringID(UILabel label, long id)
{
if (label != null)
{
label.text = DeclareMessageString.Get((int)id);
}
}
public static void SetTextByMessageStringID(UILabel label, long id, params object[] parames)
{
if (label != null)
{
label.text = string.Format(DeclareMessageString.Get((int)id), parames);
}
}
public static void SetTextByStringDefinesID(UILabel label, long id)
{
if (label != null)
{
label.text = CfgStringLua.Get((int)id);
}
}
public static void SetTextByStringDefinesID(UILabel label, long id, params object[] parames)
{
if (label != null)
{
label.text = string.Format(CfgStringLua.Get((int)id), parames);
}
}
public static void SetTextByBigNumber(UILabel label, long number, bool isUseBigUnit = false, int validCount = 0)
{
if (label != null)
{
if (isUseBigUnit)
{
label.text = CommonUtils.CovertToBigUnit(number, validCount);
}
else
{
label.text = number.ToString();
}
}
}
public static void SetTextByNumber(UILabel label, double number, bool isUseBigUnit = false, int validCount = 0)
{
if (label != null)
{
if (isUseBigUnit)
{
label.text = CommonUtils.CovertToBigUnit(number, validCount);
}
else
{
label.text = number.ToString();
}
}
}
public static void SetTextByString(UILabel label, string str)
{
if(label != null)
{
label.text = str;
}
}
public static void ClearText(UILabel label)
{
if (label != null)
{
label.text = "";
}
}
public static void SetTextFormatById(UILabel label, long formatId, long id)
{
if (label != null)
{
label.text = string.Format(CfgStringLua.Get((int)formatId), CfgStringLua.Get((int)id));
}
}
public static void SetTextFormat(UILabel label, string format, params object[] parames)
{
if (label != null)
{
label.text = string.Format(format, parames);
}
}
//设置百分比类型的Label
public static void SetTextByPercent(UILabel label, double num)
{
if (label != null)
{
label.text = string.Format(DeclareMessageString.Get(DeclareMessageString.Percent), num);
}
}
//设置进度类型的Label
public static void SetTextByProgress(UILabel label, double numLeft, double numRight, bool isUseBigUnit = false, int validCount = 0)
{
if (label != null)
{
if (isUseBigUnit)
{
label.text = string.Format(DeclareMessageString.Get(DeclareMessageString.Progress), CommonUtils.CovertToBigUnit(numLeft, validCount), CommonUtils.CovertToBigUnit(numRight, validCount));
}
else
{
label.text = string.Format(DeclareMessageString.Get(DeclareMessageString.Progress), numLeft, numRight);
}
}
}
public static void SetTextMMSS(UILabel label, double second)
{
if (label != null)
{
var lSec = (long)second;
label.text = string.Format("{0:D2}:{1:D2}", lSec / 60, lSec % 60);
}
}
public static void SetTextHHMMSS(UILabel label, double second)
{
if (label != null)
{
var ts = TimeSpan.FromSeconds(second);
label.text = string.Format("{0:D2}:{1:D2}:{2:D2}", ts.Hours + ts.Days * 24, ts.Minutes, ts.Seconds);
}
}
public static void SetTextDDHHMMSS(UILabel label, double second)
{
if (label != null)
{
var ts = TimeSpan.FromSeconds(second);
label.text = DeclareMessageString.Format(DeclareMessageString.WINGSRENT_LEFTTIME, ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
}
}
public static void SetTextYYMMDDHHMMSS(UILabel label, double second)
{
if (label != null)
{
DateTime startTime = new DateTime(1970, 1, 1);
DateTime dateTime = TimeZone.CurrentTimeZone.ToLocalTime(startTime);
dateTime = dateTime.AddSeconds(second);
label.text = string.Format("{0:D2}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);
}
}
public static void SetTextYYMMDDHHMMSSNotZone(UILabel label, double second)
{
if (label != null)
{
DateTime dateTime = new DateTime(1970, 1, 1);
dateTime = dateTime.AddSeconds(second);
label.text = string.Format("{0:D2}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);
}
}
public static void SetTextByPropName(UILabel label, long propId, string formatString)
{
if (label != null)
{
if(string.IsNullOrEmpty(formatString))
{
label.text = BattlePropTools.GetBattlePropName((int)propId);
}
else
{
label.text = string.Format(formatString, BattlePropTools.GetBattlePropName((int)propId));
}
}
}
public static void SetTextByPropValue(UILabel label, long propId, long propValue, string formatString)
{
if (label != null)
{
if(string.IsNullOrEmpty(formatString))
{
label.text = BattlePropTools.GetBattleValueText((int)propId, propValue);
}
else
{
label.text = string.Format(formatString, BattlePropTools.GetBattleValueText((int)propId, propValue));
}
}
}
public static void SetTextByPropNameAndValue(UILabel label, long propId, long propValue, string formatString)
{
if (label != null)
{
if (string.IsNullOrEmpty(formatString))
{
label.text = string.Format("{0}+{1}", BattlePropTools.GetBattlePropName((int)propId), BattlePropTools.GetBattleValueText((int)propId, propValue));
}
else
{
label.text = string.Format(formatString, BattlePropTools.GetBattlePropName((int)propId), BattlePropTools.GetBattleValueText((int)propId, propValue));
}
}
}
//获取文本内容
public static string GetText(UILabel label)
{
if (label != null)
{
return label.text;
}
return string.Empty;
}
public static FRealObjectScript FindFRealObjectScript(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<FRealObjectScript>();
}
return trans.Find(path).GetComponent<FRealObjectScript>();
}
public static UIButton FindBtn(Transform trans, string path)
{
if(string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIButton>();
}
return trans.Find(path).GetComponent<UIButton>();
}
public static UIPanel FindPanel(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIPanel>();
}
return trans.Find(path).GetComponent<UIPanel>();
}
public static UILabel FindLabel(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UILabel>();
}
return trans.Find(path).GetComponent<UILabel>();
}
public static UITexture FindTex(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UITexture>();
}
return trans.Find(path).GetComponent<UITexture>();
}
public static UITexture RequireTex(Transform trans, string path)
{
Transform tmpTrans = null;
if (string.IsNullOrEmpty(path))
tmpTrans = trans;
else
tmpTrans = trans.Find(path);
return UnityUtils.RequireComponent<UITexture>(tmpTrans.gameObject);
}
public static UISprite FindSpr(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UISprite>();
}
return trans.Find(path).GetComponent<UISprite>();
}
public static UIWidget FindWid(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIWidget>();
}
return trans.Find(path).GetComponent<UIWidget>();
}
public static UIGrid FindGrid(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIGrid>();
}
return trans.Find(path).GetComponent<UIGrid>();
}
public static UITable FindTable(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UITable>();
}
return trans.Find(path).GetComponent<UITable>();
}
public static UIToggle FindToggle(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIToggle>();
}
return trans.Find(path).GetComponent<UIToggle>();
}
public static UIScrollView FindScrollView(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIScrollView>();
}
return trans.Find(path).GetComponent<UIScrollView>();
}
public static UIProgressBar FindProgressBar(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIProgressBar>();
}
return trans.Find(path).GetComponent<UIProgressBar>();
}
public static UISlider FindSlider(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UISlider>();
}
return trans.Find(path).GetComponent<UISlider>();
}
public static UIInput FindInput(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIInput>();
}
return trans.Find(path).GetComponent<UIInput>();
}
public static TweenPosition FindTweenPosition(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<TweenPosition>();
}
return trans.Find(path).GetComponent<TweenPosition>();
}
public static TweenColor FindTweenColor(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<TweenColor>();
}
return trans.Find(path).GetComponent<TweenColor>();
}
public static TweenScale FindTweenScale(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<TweenScale>();
}
return trans.Find(path).GetComponent<TweenScale>();
}
public static TweenRotation FindTweenRotation(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<TweenRotation>();
}
return trans.Find(path).GetComponent<TweenRotation>();
}
public static TweenAlpha FindTweenAlpha(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<TweenAlpha>();
}
return trans.Find(path).GetComponent<TweenAlpha>();
}
public static Camera FindCamera(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<Camera>();
}
return trans.Find(path).GetComponent<Camera>();
}
public static BoxCollider FindBoxCollider(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<BoxCollider>();
}
return trans.Find(path).GetComponent<BoxCollider>();
}
public static UIScrollBar FindScrollBar(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIScrollBar>();
}
return trans.Find(path).GetComponent<UIScrollBar>();
}
public static UISutureTexture FindSutureTex(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UISutureTexture>();
}
return trans.Find(path).GetComponent<UISutureTexture>();
}
public static UILuaForm FindLuaForm(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UILuaForm>();
}
return trans.Find(path).GetComponent<UILuaForm>();
}
public static UIExtendLuaForm FindExtendLuaForm(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIExtendLuaForm>();
}
return trans.Find(path).GetComponent<UIExtendLuaForm>();
}
public static UIPlayerBagItem FindUIPlayerBagItem(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIPlayerBagItem>();
}
return trans.Find(path).GetComponent<UIPlayerBagItem>();
}
public static UIEventListener FindEventListener(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<UIEventListener>();
}
return trans.Find(path).GetComponent<UIEventListener>();
}
public static TweenTransform FindTweenTransform(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<TweenTransform>();
}
return trans.Find(path).GetComponent<TweenTransform>();
}
public static Transform SearchHierarchy(Transform current, string name, bool ignoreCase = false)
{
return UnityHierarchyUtils.SearchHierarchy(current, name, ignoreCase);
}
public static PlayableDirector FindPlayableDirector(Transform trans, string path)
{
if (string.IsNullOrEmpty(path))
{
return trans.GetComponent<PlayableDirector>();
}
var temp = trans.Find(path);
if (temp != null)
{
return temp.GetComponent<PlayableDirector>();
}
return null;
}
public static UIMainChat RequireUIMainChat(Transform trans)
{
var result = trans.GetComponent<UIMainChat>();
if (result == null)
{
result = trans.gameObject.AddComponent<UIMainChat>();
}
return result;
}
public static UIBlinkCompoent RequireUIBlinkCompoent(Transform tf)
{
var _componet = tf.GetComponent<UIBlinkCompoent>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UIBlinkCompoent>();
}
return _componet;
}
public static UISpriteSelectEffect RequireUISpriteSelectEffect(Transform tf)
{
var _componet = tf.GetComponent<UISpriteSelectEffect>();
if (_componet == null)
{
return tf.gameObject.AddComponent<UISpriteSelectEffect>();
}
return _componet;
}
#endregion
/// <summary>
/// 把Panel的Clip偏移归零
/// </summary>
/// <param name="panel"></param>
public static void PanelClipOffsetToZero(UIPanel panel)
{
if (panel != null)
{
var offset = panel.clipOffset;
var localpos = panel.transform.localPosition;
localpos.x += offset.x;
localpos.y += offset.y;
panel.clipOffset = Vector2.zero;
panel.transform.localPosition = localpos;
var sp = panel.GetComponent<SpringPanel>();
if (sp != null)
{
sp.target = localpos;
}
}
else
{
Debug.LogError("Panel is NULL!!!");
}
}
//yuqiang 2018/05/22 use for multi-language
public static string StripLanSymbol(string text, string lan)
{
if (string.IsNullOrEmpty(lan))
lan = UnityEngine.Gonbest.MagicCube.FLanguage.CH;
if (string.IsNullOrEmpty(text))
return text;
int offset = 0;
int startIndex = 0;
int endIndex = 0;
string startLanTag = lan;
string endLanTag = "/" + lan;
int lanLen = startLanTag.Length;
int endLanLen = endLanTag.Length;
int textLen = text.Length;
bool findStart = false;
bool findEnd = false;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (offset = 0; offset < textLen; ++offset)
{
if (offset + lanLen + 1 >= textLen) break;
if (text[offset] == '[' && text[offset + lanLen + 1] == ']')
{
for (int i = 0; i < lanLen; ++i)
{
if (text[offset + i + 1] == startLanTag[i])
{
findStart = true;
}
else
{
findStart = false;
break;
}
}
if (findStart)
{
//skip ']'
offset += lanLen + 1 + 1;
startIndex = offset;
}
}
if (offset + endLanLen + 1 >= textLen) break;
if (findStart)
{
if (text[offset] == '[' && text[offset + endLanLen + 1] == ']')
{
for (int i = 0; i < endLanLen; ++i)
{
if (text[offset + i + 1] == endLanTag[i])
{
findEnd = true;
}
else
{
findEnd = false;
break;
}
}
}
if (findEnd)
{
endIndex = offset;
if (endIndex > 0 && endIndex >= startIndex)
{
sb.Append(text.Substring(startIndex, endIndex - startIndex));
}
startIndex = endIndex;
findEnd = false;
//跳到语言tag末尾
offset += endLanLen + 1 + 1;
continue;
}
}
}
if (sb.Length > 0)
return sb.ToString();
return text;
}
}
}