1.  
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TopCameraSelect : MonoBehaviour
  6. {
  7. public static TopCameraSelect Instance;
  8. public bool disableSelect = false;
  9. public Color selectColor = Color.green;
  10. public float selectLineWidth = 2f;
  11.  
  12. public float lookDamper = 5f;
  13. public string selectionObjectName = "RTS Selection";
  14. private bool isDrag;
  15. private Vector3 selectStartPosition;
  16. private Texture2D pixel;
  17. private GameObject selection;
  18. public LinkedList<GameObject> selectObjects = new LinkedList<GameObject>();
  19. private void Awake()
  20. {
  21. Instance = this;
  22. }
  23. void Start()
  24. {
  25. setPixel(selectColor);
  26. selection = new GameObject(selectionObjectName);
  27. {
  28. var collider = selection.AddComponent<BoxCollider>() as BoxCollider;
  29. collider.isTrigger = true;
  30. Vector3 size = new Vector3(1, 100000f, 1);
  31. collider.size = size;
  32. }
  33. var body = selection.AddComponent<Rigidbody>() as Rigidbody;
  34. body.useGravity = false;
  35.  
  36. selection.SetActive(false);
  37. }
  38. void Update()
  39. {
  40. updateDragging();
  41. }
  42. public bool HasSelectObj
  43. {
  44. get
  45. {
  46. if (selectObjects.Count > 0)
  47. return true;
  48. return false;
  49. }
  50.  
  51. }
  52.  
  53. public Vector3 GetSelectObjCenter()
  54. {
  55. if (HasSelectObj)
  56. {
  57. Debug.LogError("错误");
  58. return Vector3.zero;
  59. }
  60. Vector3 vector3 = new Vector3();
  61. foreach (GameObject go in selectObjects)
  62. vector3 += go.transform.position;
  63. vector3 = vector3 / selectObjects.Count;
  64. return vector3;
  65. }
  66. void OnGUI()
  67. {
  68. if (!isDrag || disableSelect) return;
  69. var x = selectStartPosition.x;
  70. var y = Screen.height - selectStartPosition.y;
  71. var width = (Input.mousePosition - selectStartPosition).x;
  72. var height = (Screen.height - Input.mousePosition.y) - y;
  73. GUI.DrawTexture(new Rect(x, y, width, selectLineWidth), pixel);
  74. GUI.DrawTexture(new Rect(x, y, selectLineWidth, height), pixel);
  75. GUI.DrawTexture(new Rect(x, y + height, width, selectLineWidth), pixel);
  76. GUI.DrawTexture(new Rect(x + width, y, selectLineWidth, height), pixel);
  77. }
  78. public void AddSelectObj(GameObject obj)
  79. {
  80. if (selectObjects.Contains(obj))
  81. return;
  82. selectObjects.AddLast(obj);
  83. }
  84. private void setPixel(Color color)
  85. {
  86. pixel = new Texture2D(1, 1);
  87. pixel.SetPixel(0, 0, color);
  88. pixel.Apply();
  89. }
  90. private void updateDragging()
  91. {
  92. if (Input.GetMouseButtonDown(0) && !isDrag)
  93. {
  94. isDrag = true;
  95.  
  96. selectStartPosition = Input.mousePosition;
  97. if (selection != null)
  98. {
  99. selection.SetActive(true);
  100. if (selectObjects.Count > 0)
  101. {
  102. foreach (GameObject item in selectObjects)
  103. {
  104. item.GetComponent<Renderer>().material.color = Color.red;
  105. }
  106. selectObjects.Clear();
  107. }
  108. }
  109. }
  110. else if (Input.GetMouseButtonUp(0) && isDrag)
  111. {
  112. isDrag = false;
  113.  
  114. dropSelection(selectStartPosition, Input.mousePosition);
  115. if (selectObjects.Count > 0)
  116. {
  117. foreach (GameObject item in selectObjects)
  118. {
  119. item.GetComponent<Renderer>().material.color = Color.green;
  120. }
  121. }
  122. if (selection != null)
  123. {
  124. selection.SetActive(false);
  125. }
  126. }
  127. if (selection.activeSelf)
  128. {
  129. dropSelection(selectStartPosition, Input.mousePosition);
  130. }
  131. }
  132. private void dropSelection(Vector3 screenStart, Vector3 screenEnd)
  133. {
  134. var start = GetComponent<Camera>().ScreenToWorldPoint(screenStart);
  135. var finish = GetComponent<Camera>().ScreenToWorldPoint(screenEnd);
  136. selection.transform.rotation = Quaternion.Euler(transform.localEulerAngles.x - 90, transform.rotation.y, transform.rotation.z);
  137. selection.transform.position = new Vector3((start.x + finish.x) / 2.0f, (start.y + finish.y) / 2.0f, (start.z + finish.z) / 2.0f);
  138. selection.transform.localScale = new Vector3(Mathf.Abs(start.x - finish.x), 1f, Mathf.Abs(start.z - finish.z));
  139. }
  140. }
  1.  

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class TopCameraController : MonoBehaviour
  6. {
  7. public Vector2 cameraOffsetXYSpeed = Vector2.one;
  8. public Vector2 cameraOffset = Vector2.one;
  9. public LayerMask ground;
  10. public float height = 5;
  11. public float mouseWheelSpeed = 0.05f;
  12. Vector2 viewheightClimp = new Vector2(5, 15);
  13. Camera my_Camera;
  14. TopCameraSelect topCameraSelect;
  15. public static Action<Vector3> PointHit;
  16. void Awake()
  17. {
  18. my_Camera = GetComponent<Camera>();
  19. topCameraSelect = GetComponent<TopCameraSelect>();
  20. }
  21. private void Start()
  22. {
  23. my_Camera.transform.position = new Vector3(cameraOffset.x, height, cameraOffset.y);
  24.  
  25. }
  26. private void LateUpdate()
  27. {
  28. if (Input.mousePosition.x < 1)
  29. my_Camera.transform.position += new Vector3(-cameraOffsetXYSpeed.x * Time.deltaTime, 0, 0);
  30. else if (Input.mousePosition.x > Screen.width - 1)
  31. my_Camera.transform.position += new Vector3(cameraOffsetXYSpeed.x * Time.deltaTime, 0, 0);
  32. if (Input.mousePosition.y < 1)
  33. my_Camera.transform.position += new Vector3(0, 0, -cameraOffsetXYSpeed.y * Time.deltaTime);
  34. else if (Input.mousePosition.y > Screen.height - 1)
  35. my_Camera.transform.position += new Vector3(0, 0, cameraOffsetXYSpeed.y * Time.deltaTime);
  36. if (Input.GetMouseButtonDown(1))
  37. {
  38. RaycastHit hit;
  39. Ray ray = my_Camera.ScreenPointToRay(Input.mousePosition);
  40. if (Physics.Raycast(ray, out hit, 100, ground))
  41. {
  42. PointHit?.Invoke(hit.point);
  43. }
  44. }
  45. // 滚轮实现镜头缩进和拉远
  46. if (Input.GetAxis("Mouse ScrollWheel") != 0)
  47. {
  48. my_Camera.orthographicSize = my_Camera.orthographicSize - Input.GetAxis("Mouse ScrollWheel") * mouseWheelSpeed;
  49. my_Camera.orthographicSize = Mathf.Clamp(my_Camera.orthographicSize, viewheightClimp.x, viewheightClimp.y);
  50. }
  51. if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
  52. transform.position = transform.position + new Vector3(Input.GetAxis("Horizontal") * cameraOffsetXYSpeed.x * 0.1f, 0, Input.GetAxis("Vertical") * cameraOffsetXYSpeed.y * 0.1f);
  53. if (Input.GetKeyDown(KeyCode.F))
  54. {
  55. if (topCameraSelect.HasSelectObj)
  56. {
  57. Vector3 center = topCameraSelect.GetSelectObjCenter();
  58. my_Camera.transform.position = center + new Vector3(cameraOffset.x, height, cameraOffset.y);
  59. my_Camera.transform.LookAt(center);
  60. }
  61.  
  62. }
  63. }
  64. }

相机选择物体

简单RTSCamera实现的更多相关文章

  1. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  2. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  3. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  4. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  5. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  6. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  7. 使用 Nodejs 搭建简单的Web服务器

    使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...

  8. ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面

    前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...

  9. 简单入门canvas - 通过刮奖效果来学习

    一 .前言 一直在做PC端的前端开发,从互联网到行业软件.最近发现移动端已经成为前端必备技能了,真是不能停止学习.HTML5新增的一些东西,canvas是用的比较多也比较复杂的一个,简单的入门了一下, ...

随机推荐

  1. K8s之实践Pod深入理解

      K8s之实践Pod深入理解 1.同一pod下的nginx+php+mysql nginx+php+mysql.yaml文件 --- apiVersion: v1 kind: Secret meta ...

  2. 致萌新与不会用 NOI Linux 的 OIer

    全文绝大部分转载自:这篇好文章啊. 目录 1:GUIDE 2:Gedit 原文 打开 编译运行 3.Vim 3-1:这东西咋开啊 3-2:这东西咋用啊 4.编译与运行 5.调试 6.CSP竞赛中编写代 ...

  3. Exception in MIPS

    介绍 分支.跳转.异常(包括硬件中断)是三种改变控制流的事件. 同步异常是指程序执行到固定位置必定触发且每次现象一致的异常,如算术溢出异常.未定义指令异常.缺页异常等. 异步异常与当前执行程序无关,如 ...

  4. Ideas and Tricks Part II

    33.对于统计答案幂次的技巧 对于$x^k$,考虑其组合意义:将$k$个不同球放到$x$个不同的盒子里的方案数,直接维护不好维护,那么考虑枚举把这些球放到了哪些盒子里,最后乘上第二类斯特林数和对于的阶 ...

  5. Elasticsearch原理解析与性能调优

    基本概念 定义 一个分布式的实时文档存储,每个字段 可以被索引与搜索 一个分布式实时分析搜索引擎 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据 用途 全文检索 结构化搜索 分 ...

  6. 如何优雅阻止view UI 的 Switch 切换?

    一.官方文档提供的方法 个人觉得官方提供的方法有时候不能够满足现实需求,第二点是view UI版本必须是4.0.0版本及以上才能使用这个开关组件. 二.自定义方法解决 ①将开关禁用掉  加一个 dis ...

  7. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  8. 【Spring Boot】web开发

    ​ springboot在开发web项目的时候具备天然的优势,现在的很多企业级开发都是依托于springboot的. ​ 使用springboot的步骤: ​ 1.创建一个SpringBoot应用,选 ...

  9. mysql查询上月天数/当月第一天/上月第一天

    select (DATEDIFF(DATE_ADD(curdate(), INTERVAL - DAY(curdate())+ 1 DAY), date_add(curdate()- DAY(curd ...

  10. SpringBoot中的响应式web应用

    目录 简介 Reactive in Spring 注解方式使用WebFlux 编程方式使用webFlux Spring WebFlux的测试 总结 简介 在Spring 5中,Spring MVC引入 ...