Skip to content

Commit 36f9c8a

Browse files
committed
[增加]1. 增加图形释放的回调函数
1 parent 235c282 commit 36f9c8a

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

Runtime/Core/NGraphics.cs

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class NGraphics : IMeshFactory
4646
/// </summary>
4747
public event Action meshModifier;
4848

49+
/// <summary>
50+
/// 销毁触发
51+
/// </summary>
52+
public Action disposeCallback;
53+
4954
NTexture _texture;
5055
string _shader;
5156
Material _material;
@@ -66,6 +71,7 @@ public class VertexMatrix
6671
public Vector3 cameraPos;
6772
public Matrix4x4 matrix;
6873
}
74+
6975
VertexMatrix _vertexMatrix;
7076

7177
bool hasAlphaBackup;
@@ -141,6 +147,7 @@ public IMeshFactory meshFactory
141147
_meshFactory = new T();
142148
_meshDirty = true;
143149
}
150+
144151
return (T)_meshFactory;
145152
}
146153

@@ -184,13 +191,21 @@ public NTexture texture
184191
if (_texture != value)
185192
{
186193
if (value != null)
194+
{
187195
value.AddRef();
196+
}
197+
188198
if (_texture != null)
199+
{
189200
_texture.ReleaseRef();
201+
}
190202

191203
_texture = value;
192204
if (_customMatarial != 0 && _material != null)
205+
{
193206
_material.mainTexture = _texture != null ? _texture.nativeTexture : null;
207+
}
208+
194209
_meshDirty = true;
195210
UpdateManager();
196211
}
@@ -219,9 +234,13 @@ public void SetShaderAndTexture(string shader, NTexture texture)
219234
{
220235
_shader = shader;
221236
if (_texture != texture)
237+
{
222238
this.texture = texture;
239+
}
223240
else
241+
{
224242
UpdateManager();
243+
}
225244
}
226245

227246
/// <summary>
@@ -232,24 +251,33 @@ public Material material
232251
get
233252
{
234253
if (_customMatarial == 0 && _material == null && _manager != null)
254+
{
235255
_material = _manager.GetMaterial(_materialFlags, blendMode, 0);
256+
}
257+
236258
return _material;
237259
}
238260
set
239261
{
240262
if ((_customMatarial & 128) != 0 && _material != null)
263+
{
241264
Object.DestroyImmediate(_material);
265+
}
242266

243267
_material = value;
244268
if (_material != null)
245269
{
246270
_customMatarial = 1;
247271
if (_material.HasProperty(ShaderConfig.ID_Stencil) || _material.HasProperty(ShaderConfig.ID_ClipBox))
272+
{
248273
_customMatarial |= 2;
274+
}
249275

250276
meshRenderer.sharedMaterial = _material;
251277
if (_texture != null)
278+
{
252279
_material.mainTexture = _texture.nativeTexture;
280+
}
253281
}
254282
else
255283
{
@@ -320,9 +348,14 @@ public void ToggleKeyword(string keyword, bool enabled)
320348
void UpdateManager()
321349
{
322350
if (_texture != null)
351+
{
323352
_manager = _texture.GetMaterialManager(_shader);
353+
}
324354
else
355+
{
325356
_manager = null;
357+
}
358+
326359
UpdateMaterialFlags();
327360
}
328361

@@ -331,12 +364,18 @@ void UpdateMaterialFlags()
331364
if (_customMatarial != 0)
332365
{
333366
if (material != null)
367+
{
334368
material.shaderKeywords = _shaderKeywords;
369+
}
335370
}
336371
else if (_shaderKeywords != null && _manager != null)
372+
{
337373
_materialFlags = _manager.GetFlagsByKeywords(_shaderKeywords);
374+
}
338375
else
376+
{
339377
_materialFlags = 0;
378+
}
340379
}
341380

342381
/// <summary>
@@ -504,16 +543,25 @@ public void Dispose()
504543
if (mesh != null)
505544
{
506545
if (Application.isPlaying)
546+
{
507547
Object.Destroy(mesh);
548+
}
508549
else
550+
{
509551
Object.DestroyImmediate(mesh);
552+
}
553+
510554
mesh = null;
511555
}
556+
512557
if ((_customMatarial & 128) != 0 && _material != null)
558+
{
513559
Object.DestroyImmediate(_material);
560+
}
514561

515562
if (_texture != null)
516563
{
564+
disposeCallback?.Invoke();
517565
_texture.ReleaseRef();
518566
_texture = null;
519567
}
@@ -524,6 +572,7 @@ public void Dispose()
524572
meshFilter = null;
525573
_stencilEraser = null;
526574
meshModifier = null;
575+
disposeCallback = null;
527576
}
528577

529578
/// <summary>
@@ -542,7 +591,9 @@ public void Update(UpdateContext context, float alpha, bool grayed)
542591
UpdateMeshNow();
543592
}
544593
else if (_alpha != alpha)
594+
{
545595
ChangeAlpha(alpha);
596+
}
546597

547598
if (_propertyBlock != null && _blockUpdated)
548599
{
@@ -553,7 +604,9 @@ public void Update(UpdateContext context, float alpha, bool grayed)
553604
if (_customMatarial != 0)
554605
{
555606
if ((_customMatarial & 2) != 0 && _material != null)
607+
{
556608
context.ApplyClippingProperties(_material, false);
609+
}
557610
}
558611
else
559612
{
@@ -573,38 +626,57 @@ public void Update(UpdateContext context, float alpha, bool grayed)
573626
if (context.clipped)
574627
{
575628
if (context.stencilReferenceValue > 0)
629+
{
576630
matFlags |= (int)MaterialFlags.StencilTest;
631+
}
632+
577633
if (context.rectMaskDepth > 0)
578634
{
579635
if (context.clipInfo.soft)
636+
{
580637
matFlags |= (int)MaterialFlags.SoftClipped;
638+
}
581639
else
640+
{
582641
matFlags |= (int)MaterialFlags.Clipped;
642+
}
583643
}
584644

585645
_material = _manager.GetMaterial(matFlags, blendMode, context.clipInfo.clipId);
586646
if (_manager.firstMaterialInFrame)
647+
{
587648
context.ApplyClippingProperties(_material, true);
649+
}
588650
}
589651
else
652+
{
590653
_material = _manager.GetMaterial(matFlags, blendMode, 0);
654+
}
591655
}
592656
}
593657
else
658+
{
594659
_material = null;
660+
}
595661

596662
if (!Material.ReferenceEquals(_material, meshRenderer.sharedMaterial))
663+
{
597664
meshRenderer.sharedMaterial = _material;
665+
}
598666
}
599667

600668
if (_maskFlag != 0)
601669
{
602670
if (_maskFlag == 1)
671+
{
603672
_maskFlag = 2;
673+
}
604674
else
605675
{
606676
if (_stencilEraser != null)
677+
{
607678
_stencilEraser.enabled = false;
679+
}
608680

609681
_maskFlag = 0;
610682
}
@@ -622,7 +694,9 @@ internal void _PreUpdateMask(UpdateContext context, uint maskId)
622694
_stencilEraser.meshFilter.mesh = mesh;
623695
}
624696
else
697+
{
625698
_stencilEraser.enabled = true;
699+
}
626700
}
627701

628702
_maskFlag = 1;
@@ -632,7 +706,9 @@ internal void _PreUpdateMask(UpdateContext context, uint maskId)
632706
//这里使用maskId而不是clipInfo.clipId,是因为遮罩有两个用途,一个是写入遮罩,一个是擦除,两个不能用同一个材质
633707
Material mat = _manager.GetMaterial((int)MaterialFlags.AlphaMask | _materialFlags, BlendMode.Normal, maskId);
634708
if (!Material.ReferenceEquals(mat, _stencilEraser.meshRenderer.sharedMaterial))
709+
{
635710
_stencilEraser.meshRenderer.sharedMaterial = mat;
711+
}
636712

637713
context.ApplyAlphaMaskProperties(mat, true);
638714
}
@@ -649,18 +725,26 @@ void UpdateMeshNow()
649725
mesh.Clear();
650726

651727
if (meshModifier != null)
728+
{
652729
meshModifier();
730+
}
653731
}
732+
654733
return;
655734
}
656735

657736
VertexBuffer vb = VertexBuffer.Begin();
658737
vb.contentRect = _contentRect;
659738
vb.uvRect = _texture.uvRect;
660739
if (_texture != null)
740+
{
661741
vb.textureSize = new Vector2(_texture.width, _texture.height);
742+
}
662743
else
744+
{
663745
vb.textureSize = new Vector2(0, 0);
746+
}
747+
664748
if (_flip != FlipType.None)
665749
{
666750
if (_flip == FlipType.Horizontal || _flip == FlipType.Both)
@@ -669,13 +753,15 @@ void UpdateMeshNow()
669753
vb.uvRect.xMin = vb.uvRect.xMax;
670754
vb.uvRect.xMax = tmp;
671755
}
756+
672757
if (_flip == FlipType.Vertical || _flip == FlipType.Both)
673758
{
674759
float tmp = vb.uvRect.yMin;
675760
vb.uvRect.yMin = vb.uvRect.yMax;
676761
vb.uvRect.yMax = tmp;
677762
}
678763
}
764+
679765
vb.vertexColor = _color;
680766
_meshFactory.OnPopulateMesh(vb);
681767

@@ -687,8 +773,11 @@ void UpdateMeshNow()
687773
mesh.Clear();
688774

689775
if (meshModifier != null)
776+
{
690777
meshModifier();
778+
}
691779
}
780+
692781
vb.End();
693782
return;
694783
}
@@ -760,13 +849,20 @@ void UpdateMeshNow()
760849
#if UNITY_5_2 || UNITY_5_3_OR_NEWER
761850
mesh.SetVertices(vb.vertices);
762851
if (vb._isArbitraryQuad)
852+
{
763853
mesh.SetUVs(0, vb.FixUVForArbitraryQuad());
854+
}
764855
else
856+
{
765857
mesh.SetUVs(0, vb.uvs);
858+
}
859+
766860
mesh.SetColors(vb.colors);
767861
mesh.SetTriangles(vb.triangles, 0);
768862
if (vb.uvs2.Count == vb.uvs.Count)
863+
{
769864
mesh.SetUVs(1, vb.uvs2);
865+
}
770866

771867
#if !UNITY_5_6_OR_NEWER
772868
_colors = null;
@@ -797,7 +893,9 @@ void UpdateMeshNow()
797893
vb.End();
798894

799895
if (meshModifier != null)
896+
{
800897
meshModifier();
898+
}
801899
}
802900

803901
public void OnPopulateMesh(VertexBuffer vb)
@@ -809,7 +907,7 @@ public void OnPopulateMesh(VertexBuffer vb)
809907
vb._isArbitraryQuad = _vertexMatrix != null;
810908
}
811909

812-
class StencilEraser
910+
sealed class StencilEraser
813911
{
814912
public GameObject gameObject;
815913
public MeshFilter meshFilter;
@@ -839,4 +937,4 @@ public bool enabled
839937
}
840938
}
841939
}
842-
}
940+
}

0 commit comments

Comments
 (0)