这个适配是依据坐标系的象限的思想来进项适配的。參考了部分的NGUI的适配方案。

在程序的事实上,来測量UI距离相机边界的像素然后依据比例来进行适配,个人认为还不错。 放码!

有个前提哦就是你要先定一个尺寸。

假如我优先适配1024*768。那在放置这个脚本之前,要把自己的界面还成1024*768的哦。我是依据第一次来进行适配的哦。

  1. using UnityEngine;
  2. using System.Collections;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6.  
  7. public enum QuadrantLayout{
  8. Quadrant1,
  9. Quadrant2,
  10. Quadrant3,
  11. Quadrant4
  12. }
  13.  
  14. [ExecuteInEditMode]
  15. public class AutoLayout : MonoBehaviour{
  16.  
  17. [HideInInspector] public QuadrantLayout quadrant;
  18. [HideInInspector] public Vector2 margin;
  19. Vector3 lastPostion;
  20.  
  21. #if UNITY_EDITOR
  22. [HideInInspector] [SerializeField] private bool isFirstLoad=true;
  23. int pixelWidth;
  24. void Awake(){
  25. lastPostion = this.transform.localPosition;
  26. if(isFirstLoad){
  27. updateMarginOffset();
  28. isFirstLoad=false;
  29. }else{
  30. resetMarginOffset();
  31. }
  32. }
  33.  
  34. void Update(){
  35. if(!Application.isPlaying){
  36. if(Vector3.Distance(lastPostion,this.transform.localPosition)>0.001f && Selection.activeGameObject == this.gameObject){
  37. updateMarginOffset();
  38. UnityEditor.EditorUtility.SetDirty(this);
  39. }else{
  40. resetMarginOffset();
  41. }
  42. lastPostion = this.transform.localPosition;
  43. }else{
  44. if((int)Camera.main.pixelWidth!=pixelWidth){
  45. resetMarginOffset();
  46. }
  47. pixelWidth = (int)Camera.main.pixelWidth;
  48. }
  49. }
  50. #else
  51. void Start(){
  52. resetMarginOffset();
  53. }
  54. #endif
  55.  
  56. void updateMarginOffset(){
  57. float m = (Camera.main.WorldToScreenPoint(new Vector3(1,0,0))-Camera.main.WorldToScreenPoint(Vector3.zero)).x;
  58. float halfWidth=Camera.main.pixelWidth/2.0f/m;
  59. float halfHeight=Camera.main.pixelHeight/2.0f/m;
  60. Vector3 v = this.transform.position;
  61.  
  62. //1
  63. if(v.x>=0 && v.y>=0){
  64. quadrant = QuadrantLayout.Quadrant1;
  65. margin = new Vector2(halfWidth-v.x,halfHeight-v.y);
  66. //2
  67. }else if(v.x>=0 && v.y<=0){
  68. quadrant = QuadrantLayout.Quadrant2;
  69. margin = new Vector2(halfWidth-v.x,halfHeight+v.y);
  70. //3
  71. }else if(v.x<=0 && v.y<=0){
  72. quadrant = QuadrantLayout.Quadrant3;
  73. margin = new Vector2(halfWidth+v.x,halfHeight+v.y);
  74. //4
  75. }else if(v.x<=0 && v.y>=0){
  76. quadrant = QuadrantLayout.Quadrant4;
  77. margin = new Vector2(halfWidth+v.x,halfHeight-v.y);
  78. }
  79.  
  80. }
  81.  
  82. void resetMarginOffset(){
  83. Vector3 sv = Vector3.zero;
  84.  
  85. float m = (Camera.main.WorldToScreenPoint(new Vector3(1,0,0))-Camera.main.WorldToScreenPoint(Vector3.zero)).x;
  86. float halfWidth=Camera.main.pixelWidth/2.0f/m;
  87. float halfHeight=Camera.main.pixelHeight/2.0f/m;
  88.  
  89. switch(quadrant){
  90. case QuadrantLayout.Quadrant1:
  91. sv = new Vector3(halfWidth-margin.x,halfHeight-margin.y,0);
  92. break;
  93. case QuadrantLayout.Quadrant2:
  94. sv = new Vector3(halfWidth-margin.x,margin.y-halfHeight,0);
  95. break;
  96. case QuadrantLayout.Quadrant3:
  97. sv = new Vector3(margin.x-halfWidth,margin.y-halfHeight,0);
  98. break;
  99. case QuadrantLayout.Quadrant4:
  100. sv = new Vector3(margin.x-halfWidth,halfHeight-margin.y,0);
  101. break;
  102. }
  103.  
  104. sv.z = this.transform.position.z;
  105.  
  106. transform.position = sv;
  107. }
  108. }

这里先发布一个,自己写得unityUI的适配的方案(插播)的更多相关文章

  1. 如何发布一个Mac应用并使其成为全球付费榜第一

    Readdle公司如何发布第一个 Mac App,并使之成为Mac App Store 全球付费排名第一的 Easy注:自从发布了<程序员如何优雅的挣零花钱?>后,就不断有同学询问怎么做A ...

  2. 如何发布一个自定义Node.js模块到NPM(详细步骤)

    咱们闲话不多说,直接开始! 由于我从没有使用过MAC,所以我不保证本文中介绍的操作与MAC一致. 文章开始我先假定各位已经在window全局安装了Node.js,下面开始进行详细步骤介绍: 本文本着, ...

  3. 使用gitbook 发布一个教程文档网站

    gitbook是一个好用的发布电子书的项目:使用gitbook 可以在本地写好文档再远程推送到库:也可以在gitbook提供的在线平台上制作电子书:要想在自己的服务器上使用gitbook 发布一个网站 ...

  4. 教你一步步发布一个开源库到 JCenter

    今天想来分享下,如何一步步自己发布一个开源库到 JCenter 这方面的博客网上已经特别多了,所以本篇并不打算仅仅只是记录流程步骤而已,而是尽可能讲清楚,为什么需要有这个步骤,让大伙知其然的同时还知其 ...

  5. 如何发布一个npm包(基于vue)

    前言:工作的时候总是使用别人的npm包,然而我有时心底会好奇自己如何发布一个npm包呢,什么时候自己的包能够被很多人喜欢并使用呢...今天我终于迈出了第一步. 前提:会使用 npm,有 vue 基础, ...

  6. 微信小程序发布一个月,世界并没有什么不同

    从某种意义上说,在张小龙身上,最可怕的事情莫过于微信小程序发布一个月,一开始的大红大紫居然渐归沉寂,曾经的风光无限已无人谈起,世界并没有什么不同. 这真像一场噩梦,一切都可怕地颠倒了.一款微信的战略级 ...

  7. 如何发布一个自定义Node.js模块到NPM(详细步骤,附Git使用方法)

    咱们闲话不多说,直接开始! 由于我从没有使用过MAC,所以我不保证本文中介绍的操作与MAC一致. 文章开始我先假定各位已经在window全局安装了Node.js,下面开始进行详细步骤介绍: 本文本着, ...

  8. 从0到1发布一个npm包

    从0到1发布一个npm包 author: @TiffanysBear 最近在项目业务中有遇到一些问题,一些通用的方法或者封装的模块在PC.WAP甚至是APP中都需要使用,但是对于业务的PC.WAP.A ...

  9. 学习python这么久,有没有考虑发布一个属于自己的模块?

    ​ 1. 为什么需要对项目分发打包? 平常我们习惯了使用 pip 来安装一些第三方模块,这个安装过程之所以简单,是因为模块开发者为我们默默地为我们做了所有繁杂的工作,而这个过程就是 打包. 打包,就是 ...

随机推荐

  1. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...

  2. npm 安装或更新模块失败的解决办法

    头一次关注npm,在刚刚安装的机子上使用更新指令却报错,我还以为是SHA512有什么问题,后来发现是因为一些网络原因,所以,如果出现错误,务必修改默认配置为国内镜像,但是在publish之前,记得要改 ...

  3. django 在python 3中提示 无法找到 MySQLDB

    一直百思不得其解,为什么会建立表失败,知道我看到了这个回答... I had luck with simply pip install mysqlclient in my python3.4 virt ...

  4. 用Chrome在手机上调试本地网页代码

    本文摘自Google 原文地址1:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/?utm_sourc ...

  5. [Sgu395][bzoj2363]Binary Cat Club

    一道神题…… rzO 发现立杰在初三(http://hi.baidu.com/wjbzbmr/item/4a50c7d8a8114911d78ed0a9据此可以推断)就怒A了此题…… Orz /*** ...

  6. [BZOJ1860][ZJOI2006]Mahjong(DP)

    1860: [Zjoi2006]Mahjong麻将 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 412  Solved: 248[Submit][Sta ...

  7. 【最小生成树】【kruscal】hdu4786 Fibonacci Tree

    假设这张图能够形成具有k条白边的生成树, 则易证k一定形成一个连续的区间[a,b],中间一定不会断开.要是断开……tm怎么可能. 所以求出a,b就好啦,人家都给你把白边赋成1了,直接跑一下最小生成树, ...

  8. Android工具:Hierarchy Viewer

    Hierarchy Viewer 用途: 即可以用来优化自己的布局,也可以用来参考别人优秀的布局 打开方式: 运行工程,然后在\android-sdk-windows\tools目录下双击hierar ...

  9. [转]提示错误 package javax.servlet.jsp does not exist package javax.servletr.jsp.tagext does not exist

    你在JAVA servlet容器运行的时候没配置servlet-api.jar,tools.jar,rt.jar,jsp-api.jar的classpath 我的classpath= .;%JAVA_ ...

  10. Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset

    G. Yash And Trees 题目连接: http://www.codeforces.com/contest/633/problem/G Description Yash loves playi ...