简单RTSCamera实现
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TopCameraSelect : MonoBehaviour
- {
- public static TopCameraSelect Instance;
- public bool disableSelect = false;
- public Color selectColor = Color.green;
- public float selectLineWidth = 2f;
- public float lookDamper = 5f;
- public string selectionObjectName = "RTS Selection";
- private bool isDrag;
- private Vector3 selectStartPosition;
- private Texture2D pixel;
- private GameObject selection;
- public LinkedList<GameObject> selectObjects = new LinkedList<GameObject>();
- private void Awake()
- {
- Instance = this;
- }
- void Start()
- {
- setPixel(selectColor);
- selection = new GameObject(selectionObjectName);
- {
- var collider = selection.AddComponent<BoxCollider>() as BoxCollider;
- collider.isTrigger = true;
- Vector3 size = new Vector3(1, 100000f, 1);
- collider.size = size;
- }
- var body = selection.AddComponent<Rigidbody>() as Rigidbody;
- body.useGravity = false;
- selection.SetActive(false);
- }
- void Update()
- {
- updateDragging();
- }
- public bool HasSelectObj
- {
- get
- {
- if (selectObjects.Count > 0)
- return true;
- return false;
- }
- }
- public Vector3 GetSelectObjCenter()
- {
- if (HasSelectObj)
- {
- Debug.LogError("错误");
- return Vector3.zero;
- }
- Vector3 vector3 = new Vector3();
- foreach (GameObject go in selectObjects)
- vector3 += go.transform.position;
- vector3 = vector3 / selectObjects.Count;
- return vector3;
- }
- void OnGUI()
- {
- if (!isDrag || disableSelect) return;
- var x = selectStartPosition.x;
- var y = Screen.height - selectStartPosition.y;
- var width = (Input.mousePosition - selectStartPosition).x;
- var height = (Screen.height - Input.mousePosition.y) - y;
- GUI.DrawTexture(new Rect(x, y, width, selectLineWidth), pixel);
- GUI.DrawTexture(new Rect(x, y, selectLineWidth, height), pixel);
- GUI.DrawTexture(new Rect(x, y + height, width, selectLineWidth), pixel);
- GUI.DrawTexture(new Rect(x + width, y, selectLineWidth, height), pixel);
- }
- public void AddSelectObj(GameObject obj)
- {
- if (selectObjects.Contains(obj))
- return;
- selectObjects.AddLast(obj);
- }
- private void setPixel(Color color)
- {
- pixel = new Texture2D(1, 1);
- pixel.SetPixel(0, 0, color);
- pixel.Apply();
- }
- private void updateDragging()
- {
- if (Input.GetMouseButtonDown(0) && !isDrag)
- {
- isDrag = true;
- selectStartPosition = Input.mousePosition;
- if (selection != null)
- {
- selection.SetActive(true);
- if (selectObjects.Count > 0)
- {
- foreach (GameObject item in selectObjects)
- {
- item.GetComponent<Renderer>().material.color = Color.red;
- }
- selectObjects.Clear();
- }
- }
- }
- else if (Input.GetMouseButtonUp(0) && isDrag)
- {
- isDrag = false;
- dropSelection(selectStartPosition, Input.mousePosition);
- if (selectObjects.Count > 0)
- {
- foreach (GameObject item in selectObjects)
- {
- item.GetComponent<Renderer>().material.color = Color.green;
- }
- }
- if (selection != null)
- {
- selection.SetActive(false);
- }
- }
- if (selection.activeSelf)
- {
- dropSelection(selectStartPosition, Input.mousePosition);
- }
- }
- private void dropSelection(Vector3 screenStart, Vector3 screenEnd)
- {
- var start = GetComponent<Camera>().ScreenToWorldPoint(screenStart);
- var finish = GetComponent<Camera>().ScreenToWorldPoint(screenEnd);
- selection.transform.rotation = Quaternion.Euler(transform.localEulerAngles.x - 90, transform.rotation.y, transform.rotation.z);
- selection.transform.position = new Vector3((start.x + finish.x) / 2.0f, (start.y + finish.y) / 2.0f, (start.z + finish.z) / 2.0f);
- selection.transform.localScale = new Vector3(Mathf.Abs(start.x - finish.x), 1f, Mathf.Abs(start.z - finish.z));
- }
- }
using System;- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TopCameraController : MonoBehaviour
- {
- public Vector2 cameraOffsetXYSpeed = Vector2.one;
- public Vector2 cameraOffset = Vector2.one;
- public LayerMask ground;
- public float height = 5;
- public float mouseWheelSpeed = 0.05f;
- Vector2 viewheightClimp = new Vector2(5, 15);
- Camera my_Camera;
- TopCameraSelect topCameraSelect;
- public static Action<Vector3> PointHit;
- void Awake()
- {
- my_Camera = GetComponent<Camera>();
- topCameraSelect = GetComponent<TopCameraSelect>();
- }
- private void Start()
- {
- my_Camera.transform.position = new Vector3(cameraOffset.x, height, cameraOffset.y);
- }
- private void LateUpdate()
- {
- if (Input.mousePosition.x < 1)
- my_Camera.transform.position += new Vector3(-cameraOffsetXYSpeed.x * Time.deltaTime, 0, 0);
- else if (Input.mousePosition.x > Screen.width - 1)
- my_Camera.transform.position += new Vector3(cameraOffsetXYSpeed.x * Time.deltaTime, 0, 0);
- if (Input.mousePosition.y < 1)
- my_Camera.transform.position += new Vector3(0, 0, -cameraOffsetXYSpeed.y * Time.deltaTime);
- else if (Input.mousePosition.y > Screen.height - 1)
- my_Camera.transform.position += new Vector3(0, 0, cameraOffsetXYSpeed.y * Time.deltaTime);
- if (Input.GetMouseButtonDown(1))
- {
- RaycastHit hit;
- Ray ray = my_Camera.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit, 100, ground))
- {
- PointHit?.Invoke(hit.point);
- }
- }
- // 滚轮实现镜头缩进和拉远
- if (Input.GetAxis("Mouse ScrollWheel") != 0)
- {
- my_Camera.orthographicSize = my_Camera.orthographicSize - Input.GetAxis("Mouse ScrollWheel") * mouseWheelSpeed;
- my_Camera.orthographicSize = Mathf.Clamp(my_Camera.orthographicSize, viewheightClimp.x, viewheightClimp.y);
- }
- if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
- transform.position = transform.position + new Vector3(Input.GetAxis("Horizontal") * cameraOffsetXYSpeed.x * 0.1f, 0, Input.GetAxis("Vertical") * cameraOffsetXYSpeed.y * 0.1f);
- if (Input.GetKeyDown(KeyCode.F))
- {
- if (topCameraSelect.HasSelectObj)
- {
- Vector3 center = topCameraSelect.GetSelectObjCenter();
- my_Camera.transform.position = center + new Vector3(cameraOffset.x, height, cameraOffset.y);
- my_Camera.transform.LookAt(center);
- }
- }
- }
- }
相机选择物体
简单RTSCamera实现的更多相关文章
- 【造轮子】打造一个简单的万能Excel读写工具
大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...
- Fabio 安装和简单使用
Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
- 在Openfire上弄一个简单的推送系统
推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...
- 我的MYSQL学习心得(一) 简单语法
我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- 使用 Nodejs 搭建简单的Web服务器
使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...
- ASP.NET Aries 入门开发教程2:配置出一个简单的列表页面
前言: 朋友们都期待我稳定地工作,但创业公司若要躺下,也非意念可控. 若人生注定了风雨飘摇,那就雨中前行了. 最机开始看聊新的工作机会,欢迎推荐,创业公司也可! 同时,趁着自由时间,抓紧把这系列教程给 ...
- 简单入门canvas - 通过刮奖效果来学习
一 .前言 一直在做PC端的前端开发,从互联网到行业软件.最近发现移动端已经成为前端必备技能了,真是不能停止学习.HTML5新增的一些东西,canvas是用的比较多也比较复杂的一个,简单的入门了一下, ...
随机推荐
- K8s之实践Pod深入理解
K8s之实践Pod深入理解 1.同一pod下的nginx+php+mysql nginx+php+mysql.yaml文件 --- apiVersion: v1 kind: Secret meta ...
- 致萌新与不会用 NOI Linux 的 OIer
全文绝大部分转载自:这篇好文章啊. 目录 1:GUIDE 2:Gedit 原文 打开 编译运行 3.Vim 3-1:这东西咋开啊 3-2:这东西咋用啊 4.编译与运行 5.调试 6.CSP竞赛中编写代 ...
- Exception in MIPS
介绍 分支.跳转.异常(包括硬件中断)是三种改变控制流的事件. 同步异常是指程序执行到固定位置必定触发且每次现象一致的异常,如算术溢出异常.未定义指令异常.缺页异常等. 异步异常与当前执行程序无关,如 ...
- Ideas and Tricks Part II
33.对于统计答案幂次的技巧 对于$x^k$,考虑其组合意义:将$k$个不同球放到$x$个不同的盒子里的方案数,直接维护不好维护,那么考虑枚举把这些球放到了哪些盒子里,最后乘上第二类斯特林数和对于的阶 ...
- Elasticsearch原理解析与性能调优
基本概念 定义 一个分布式的实时文档存储,每个字段 可以被索引与搜索 一个分布式实时分析搜索引擎 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据 用途 全文检索 结构化搜索 分 ...
- 如何优雅阻止view UI 的 Switch 切换?
一.官方文档提供的方法 个人觉得官方提供的方法有时候不能够满足现实需求,第二点是view UI版本必须是4.0.0版本及以上才能使用这个开关组件. 二.自定义方法解决 ①将开关禁用掉 加一个 dis ...
- 【SpringBoot】07.SpringBoot文件上传
SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...
- 【Spring Boot】web开发
springboot在开发web项目的时候具备天然的优势,现在的很多企业级开发都是依托于springboot的. 使用springboot的步骤: 1.创建一个SpringBoot应用,选 ...
- mysql查询上月天数/当月第一天/上月第一天
select (DATEDIFF(DATE_ADD(curdate(), INTERVAL - DAY(curdate())+ 1 DAY), date_add(curdate()- DAY(curd ...
- SpringBoot中的响应式web应用
目录 简介 Reactive in Spring 注解方式使用WebFlux 编程方式使用webFlux Spring WebFlux的测试 总结 简介 在Spring 5中,Spring MVC引入 ...