| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- local CameraShotHelper = {}
- function CameraShotHelper:SaveShotImage(tBytes, szImageName)
- local szPath = require("Base/Utils.lua"):TimeMonthFormat()
- local szFileDirectory = CS.UnityEngine.Application.dataPath .. "/" .. szPath
- if not CS.System.IO.Directory.Exists(szFileDirectory) then
- CS.System.IO.Directory.CreateDirectory(szFileDirectory)
- end
- local szFilePath = szFileDirectory .. "/" ..szImageName .. ".png"
- CS.System.IO.File.WriteAllBytes(szFilePath, tBytes)
- end
- local function GetShotImage(cameraObj, nWidth, nHeight)
- local cameraComponent = cameraObj.transform:GetComponent("Camera")
- local renderTexture = CS.UnityEngine.RenderTexture(nWidth, nHeight, 0)
- cameraComponent.targetTexture = renderTexture
- cameraComponent:Render()
- CS.UnityEngine.RenderTexture.active = renderTexture
- local texture2D = CS.UnityEngine.Texture2D(nWidth, nHeight,
- CS.UnityEngine.TextureFormat.ARGB32, false)
- local rect = CS.UnityEngine.Rect(0, 0, nWidth, nHeight)
- texture2D:ReadPixels(rect, 0, 0)
- texture2D:Apply()
- cameraComponent.targetTexture = nil
- CS.UnityEngine.RenderTexture.active = nil
- CS.UnityEngine.GameObject.Destroy(renderTexture)
- return texture2D
- end
- function CameraShotHelper:CaptureCameraShot(cameraObj, nWidth, nHeight, szImageName)
- local texture2D = GetShotImage(cameraObj, nWidth, nHeight)
- local tBytes = texture2D:EncodeToPNG()
- -- save image
- if szImageName then
- CameraShotHelper:SaveShotImage(tBytes, szImageName)
- end
- return texture2D
- end
- return CameraShotHelper
|